r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

GET request with an empty header #729

Closed ammaraziz closed 1 year ago

ammaraziz commented 1 year ago

How can I use httr::GET to send empty headers?

The documentation for add_header says:

 ...  named header values. To stop an existing header from being set, pass an empty string: "".

For example, a 'normal' header:

r <- GET("http://httpbin.org/get", add_headers(Name='hadley'))
str(content(r)$headers)

List of 6
 $ Accept         : chr "application/json, text/xml, application/xml, */*"
 $ Accept-Encoding: chr "deflate, gzip"
 $ Host           : chr "httpbin.org"
 $ Name           : chr "hadley"
 $ User-Agent     : chr "libcurl/7.64.1 r-curl/4.3.3 httr/1.4.4"
 $ X-Amzn-Trace-Id: chr "Root=1-63947737-4ad57825480901485249f1f0"

Empty header is not sent because it is unset:

r <- GET("http://httpbin.org/get", add_headers(Name=''))
str(content(r)$headers)

List of 5
 $ Accept         : chr "application/json, text/xml, application/xml, */*"
 $ Accept-Encoding: chr "deflate, gzip"
 $ Host           : chr "httpbin.org"
 $ User-Agent     : chr "libcurl/7.64.1 r-curl/4.3.3 httr/1.4.4"
 $ X-Amzn-Trace-Id: chr "Root=1-63947862-4dddca9d5789c505444e4279"

Using NULL produce the same result (no header is sent) and NA sends 'NA' as a header value. I found a similar issue (though I lost it sorry) with POST, the suggestion was to use encode = 'raw' though this does not solve the issue:


 version  R version 4.2.1 (2022-06-23)
 os       macOS Big Sur 11.6.8
 package     * version    date (UTC) lib source
 askpass       1.1        2019-01-13 [1] CRAN (R 4.2.0)
 cli           3.4.1      2022-09-23 [1] CRAN (R 4.2.0)
 curl          4.3.3      2022-10-06 [1] CRAN (R 4.2.0)
 fuzee       * 0.0.0.1000 2022-11-13 [1] Github (ammaraziz/fuzee@fd4ab8d)
 httr        * 1.4.4      2022-08-17 [1] CRAN (R 4.2.0)
 jsonlite      1.8.3      2022-10-21 [1] CRAN (R 4.2.0)
 openssl       2.0.4      2022-10-17 [1] CRAN (R 4.2.0)
 pacman        0.5.1      2019-03-11 [1] CRAN (R 4.2.0)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.2.0)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.2.0)
hadley commented 1 year ago

httr has been superseded in favour of httr2, so is no longer under active development. If this problem is still important to you in httr2, I'd suggest filing an issue offer there 😄. Thanks for using httr!