federicotdn / verb

Organize and send HTTP requests from Emacs
https://melpa.org/#/verb
GNU General Public License v3.0
540 stars 20 forks source link

Multiple headers in a variable #45

Closed MichaelXavier closed 2 years ago

MichaelXavier commented 2 years ago

Hi! I'm using verb from babel and I was wondering if there was any way to take a table, alist, or even just a formatted string with newlines and use it to specify multiple headers. I'm building a hierarchy of org headings where the toplevel one might define a few basic headers, then lower level ones set up multiple auth headers. Ideally, I'd like to avoid having to reiterate every header name explicitly in every block, e.g.:

#+BEGIN_SRC verb :wrap src ob-verb-response
GET https://example.com
{{(verb-var base-headers)}}
{{(verb-var auth-headers)}}
x-endpoint-specific-header: blah
#+END_SRC

Where base-headers might evaluate to

X-Foo: foo
X-Bar: bar

Is this possible to do?

federicotdn commented 2 years ago

Requests defined within Babel source blocks behave the same way as normal requests definitions - so you should be able to just define your headers on a top-level Org heading, and then have your Babel requests defined below:

* Main title
template
My-Header: xyz
My-Other-Header: abc

** Sub-request 1
#+BEGIN_SRC verb :wrap src ob-verb-response
GET https://example.com
x-endpoint-specific-header: blah
#+END_SRC

** Sub-request 2
#+BEGIN_SRC verb :wrap src ob-verb-response
GET https://example.com
x-endpoint-specific-header: blah
#+END_SRC

So there, My-Header: xyz and My-Other-Header: abc will be shared among the two sub requests.

MichaelXavier commented 2 years ago

That works great. Thanks!