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: 2021-03-29 17:14 #> Status: 200 #> Content-Type: application/json #> Size: 289 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.9000", #> "X-Amzn-Trace-Id": "Root=1-60620b06-586a44a93ebf7abf1eb845c9" #> } #> }
GET("http://httpbin.org/headers", accept_json())
#> Response [http://httpbin.org/headers] #> Date: 2021-03-29 17:14 #> Status: 200 #> Content-Type: application/json #> Size: 257 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.9000", #> "X-Amzn-Trace-Id": "Root=1-60620b06-3fc562a56c5fcdc118537f9a" #> } #> }
GET("http://httpbin.org/headers", accept("text/csv"))
#> Response [http://httpbin.org/headers] #> Date: 2021-03-29 17:14 #> Status: 200 #> Content-Type: application/json #> Size: 249 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.9000", #> "X-Amzn-Trace-Id": "Root=1-60620b06-7aeb5e93675ac2ed29556c22" #> } #> }
GET("http://httpbin.org/headers", accept(".doc"))
#> Response [http://httpbin.org/headers] #> Date: 2021-03-29 17:14 #> Status: 200 #> Content-Type: application/json #> Size: 259 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.9000", #> "X-Amzn-Trace-Id": "Root=1-60620b06-46f9d51464e0358105e243d9" #> } #> }
GET("http://httpbin.org/headers", content_type_xml())
#> Response [http://httpbin.org/headers] #> Date: 2021-03-29 17:14 #> Status: 200 #> Content-Type: application/json #> Size: 329 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.9000", #> "X-Amzn-Trace-Id": "Root=1-60620b06-517de4ba7e1f619c70ebac99" #> } #> }
GET("http://httpbin.org/headers", content_type("text/csv"))
#> Response [http://httpbin.org/headers] #> Date: 2021-03-29 17:14 #> Status: 200 #> Content-Type: application/json #> Size: 322 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.9000", #> "X-Amzn-Trace-Id": "Root=1-60620b06-15e89bb5230ea35427f05246" #> } #> }
GET("http://httpbin.org/headers", content_type(".xml"))
#> Response [http://httpbin.org/headers] #> Date: 2021-03-29 17:14 #> Status: 200 #> Content-Type: application/json #> Size: 329 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.9000", #> "X-Amzn-Trace-Id": "Root=1-60620b06-33cbb2496d44c859285d9e75" #> } #> }