Difference between request and body in httr :: POST

I want to pass several message parameters to url, say two parameters p1 and p2. The values ​​of p1 and p2 are xyz (string) and 1 (numeric). What is the difference between the following commands:

POST(url, body=list(p1="xyz",p2=1))

OR

POST(url, query=list(p1="xyz",p2=1))

I also cannot figure out whether to use a quote for parameters p1 and p2 or not. If so, which one or double.

+4
source share
2 answers

If you are starting with httr and the API as a whole, I highly recommend that you find out how HTTP requests are structured. One way to do this empirically is using http://httpbin.org and verbose():

library(httr)

args <- list(p1 = "xyz", p2 = 1)

POST("http://httpbin.org/post", query = args, verbose())
#-> POST /post?p1=xyz&p2=1 HTTP/1.1
#-> Host: httpbin.org
#-> Content-Length: 0

POST("http://httpbin.org/post", body = args, verbose())
#-> POST /post HTTP/1.1
#-> Host: httpbin.org
#-> Content-Length: 232
#-> Expect: 100-continue
#-> Content-Type: multipart/form-data; boundary=---03a3f580d7af2b29
#-> 
#>> ---03a3f580d7af2b29
#>> Content-Disposition: form-data; name="p1"
#>> 
#>> xyz
#>> ---03a3f580d7af2b29
#>> Content-Disposition: form-data; name="p2"
#>> 
#>> 1
#>> ---03a3f580d7af2b29--

, . query url, body HTTP-.

encode -:

POST("http://httpbin.org/post", body = args, verbose(), encode = "form")
#-> POST /post HTTP/1.1
#-> Host: httpbin.org
#-> Content-Type: application/x-www-form-urlencoded
#-> Content-Length: 11
#-> 
#>> p1=xyz&p2=1

POST("http://httpbin.org/post", body = args, verbose(), encode = "json")
#-> POST /post HTTP/1.1
#-> Host: httpbin.org
#-> Content-Type: application/json
#-> Content-Length: 19
#-> 
#>> {"p1":"xyz","p2":1}

( User-Agent, Accept-Encoding Accept , )

+5

, , . , ; .

() : POST(paste0(url, "?p1=xyz&p2=1")), () - HTTP.

+1

Source: https://habr.com/ru/post/1608685/


All Articles