These are convenient wrappers aroud add_headers().

content_type(type)

content_type_json()

content_type_xml()

accept(type)

accept_json()

accept_xml()

Arguments

type

A mime type or a file extension. If a file extension (i.e. starts with .) will guess the mime type using mime::guess_type().

Details

accept_json/accept_xml and content_type_json/content_type_xml are useful shortcuts to ask for json or xml responses or tell the server you are sending json/xml.

Examples

GET("http://httpbin.org/headers")
#> Response [http://httpbin.org/headers] #> Date: 2020-08-17 22:22 #> Status: 200 #> Content-Type: application/json #> Size: 284 B #> { #> "headers": { #> "Accept": "application/json, text/xml, application/xml, */*", #> "Accept-Encoding": "deflate, gzip", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2", #> "X-Amzn-Trace-Id": "Root=1-5f3b0328-39ffd32c9a253f18082e3004" #> } #> }
GET("http://httpbin.org/headers", accept_json())
#> Response [http://httpbin.org/headers] #> Date: 2020-08-17 22:22 #> Status: 200 #> Content-Type: application/json #> Size: 252 B #> { #> "headers": { #> "Accept": "application/json", #> "Accept-Encoding": "deflate, gzip", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2", #> "X-Amzn-Trace-Id": "Root=1-5f3b0328-cff42f7cf7377448e9f48cd4" #> } #> }
GET("http://httpbin.org/headers", accept("text/csv"))
#> Response [http://httpbin.org/headers] #> Date: 2020-08-17 22:22 #> Status: 200 #> Content-Type: application/json #> Size: 244 B #> { #> "headers": { #> "Accept": "text/csv", #> "Accept-Encoding": "deflate, gzip", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2", #> "X-Amzn-Trace-Id": "Root=1-5f3b0328-bc74e534c00c307c3e61385c" #> } #> }
GET("http://httpbin.org/headers", accept(".doc"))
#> Response [http://httpbin.org/headers] #> Date: 2020-08-17 22:22 #> Status: 200 #> Content-Type: application/json #> Size: 254 B #> { #> "headers": { #> "Accept": "application/msword", #> "Accept-Encoding": "deflate, gzip", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2", #> "X-Amzn-Trace-Id": "Root=1-5f3b0328-0001a580fe9c2af0fefeb3a8" #> } #> }
GET("http://httpbin.org/headers", content_type_xml())
#> Response [http://httpbin.org/headers] #> Date: 2020-08-17 22:22 #> Status: 200 #> Content-Type: application/json #> Size: 324 B #> { #> "headers": { #> "Accept": "application/json, text/xml, application/xml, */*", #> "Accept-Encoding": "deflate, gzip", #> "Content-Type": "application/xml", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2", #> "X-Amzn-Trace-Id": "Root=1-5f3b0328-ffec98780922c1b8c5e54ba8" #> } #> }
GET("http://httpbin.org/headers", content_type("text/csv"))
#> Response [http://httpbin.org/headers] #> Date: 2020-08-17 22:22 #> Status: 200 #> Content-Type: application/json #> Size: 317 B #> { #> "headers": { #> "Accept": "application/json, text/xml, application/xml, */*", #> "Accept-Encoding": "deflate, gzip", #> "Content-Type": "text/csv", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2", #> "X-Amzn-Trace-Id": "Root=1-5f3b0328-3c878f84e8e1fe78cd548040" #> } #> }
GET("http://httpbin.org/headers", content_type(".xml"))
#> Response [http://httpbin.org/headers] #> Date: 2020-08-17 22:22 #> Status: 200 #> Content-Type: application/json #> Size: 324 B #> { #> "headers": { #> "Accept": "application/json, text/xml, application/xml, */*", #> "Accept-Encoding": "deflate, gzip", #> "Content-Type": "application/xml", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2", #> "X-Amzn-Trace-Id": "Root=1-5f3b0328-3cd63a38988c6b2cb4fd4784" #> } #> }