Skip to content

Loading…

Allow requests to have multiple headers with the same name #101

Merged
merged 1 commit into from

2 participants

@kanaka

Legitimate HTTP requests can have the same header name repeated in the request but currently, clj-http does not allow this in the request (only in the response from what I can tell). For example, when interacting with Riak, you often have occasion to set index tags using headers and want to have multiple values for the same header name.

To make a request with multiple same named headers, use a collection
for the value.

(http/get url {:debug true :headers {"x-myheader" ["value1" "value2"]}})

...
HttpRequest:
{...
:allHeaders
[...
#,
#],
...}

@kanaka kanaka Allow requests to have multiple headers with the same name.
To make a request with multiple same named headers, use a collection
for the value.

(http/get url {:debug true :headers {"x-myheader" ["value1" "value2"]}})

...
HttpRequest:
{...
 :allHeaders
 [...
  #<BasicHeader x-myheader: value1>,
  #<BasicHeader x-myheader: value2>],
 ...}
bc65f1c
@dakrone
Owner

Good call, I'll merge this and add some tests, thanks.

@dakrone dakrone merged commit bc65f1c into dakrone:master

1 check passed

Details default The Travis build passed
@dakrone
Owner

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 19, 2012
  1. @kanaka

    Allow requests to have multiple headers with the same name.

    kanaka committed
    To make a request with multiple same named headers, use a collection
    for the value.
    
    (http/get url {:debug true :headers {"x-myheader" ["value1" "value2"]}})
    
    ...
    HttpRequest:
    {...
     :allHeaders
     [...
      #<BasicHeader x-myheader: value1>,
      #<BasicHeader x-myheader: value2>],
     ...}
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 1 deletion.
  1. +4 −1 src/clj_http/core.clj
View
5 src/clj_http/core.clj
@@ -211,7 +211,10 @@
(when (instance? SingleClientConnManager conn-mgr)
(.addHeader http-req "Connection" "close"))
(doseq [[header-n header-v] headers]
- (.addHeader http-req header-n header-v))
+ (if (coll? header-v)
+ (doseq [header-vth header-v]
+ (.addHeader http-req header-n header-vth))
+ (.addHeader http-req header-n header-v)))
(if multipart
(.setEntity #^HttpEntityEnclosingRequest http-req
(mp/create-multipart-entity multipart))
Something went wrong with that request. Please try again.