Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Easy way to obtain data from response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
yalexwander authored and pashky committed Oct 10, 2023
1 parent a21ee50 commit e2a2b13
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/httpbin
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,11 @@ GET http://httpbin.org/response-headers?Content-Type=application/vnd.whatever%2B
GET http://httpbin.org/basic-auth/jack/secret
Authorization: :auth-token
-> run-hook (message (format "authenticated with '%s'" (restclient-get-var ":auth-token")))

# saving header like csrf token for future usage
GET http://some.api/csrf_token
-> run-hook (restclient-set-var-from-header ":csrftoken" "X-CSRF-Token")

# use save header
POST http://some.api/restricted
X-CSRF-Token: :csrftoken
11 changes: 11 additions & 0 deletions restclient.el
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,17 @@ The buffer contains the raw HTTP response sent by the server."
start (match-end 0)))
headers))

(defun restclient-get-response-headers ()
"Returns alist of current response headers. Works *only* with with
hook called from `restclient-http-send-current-raw', usually
bound to C-c C-r."
(let ((start (point-min))
(headers-end (+ 1 (string-match "\n\n" (buffer-substring-no-properties (point-min) (point-max))))))
(restclient-parse-headers (buffer-substring-no-properties start headers-end))))

(defun restclient-set-var-from-header (var header)
(restclient-set-var var (cdr (assoc header (restclient-get-response-headers)))))

(defun restclient-read-file (path)
(with-temp-buffer
(insert-file-contents path)
Expand Down

0 comments on commit e2a2b13

Please sign in to comment.