Send a POST request in R package

时间:2019-01-15 18:10:00

标签: r http post request

I've been trying to use httr package in R to send a POST request

The website which I'm trying to send a POST request states to do the following:

POST /newwebservices/locationverifier.asmx/findLocation2 HTTP/1.1
Host: citizenatlas.dc.gov
Content-Type: application/x-www-form-urlencoded
Content-Length: length
str=string&f=string

where str takes an address in Washington DC such as "701 FARADAY PL NE, WASHINGTON, DC 20017" and f takes the format requested in this case I want "json"

I tried :

url = "/newwebservices/locationverifier.asmx  HTTP/1.1"
body = list(
"Host" = "citizenatlas.dc.gov",
"Content-Type" = "application/x-www-form-urlencoded",
"Content-Length" =  length,
"str"="3513 S St NW",
"f"="json"
)
httr::POST(url,body, verbose)

I also tried Get This is what they state to put in

GET /newwebservices/locationverifier.asmx/findLocation2?    str=string&f=string HTTP/1.1
Host: citizenatlas.dc.gov

or

   GET("http://citizenatlas.dc.gov/newwebservices/locationverifier.asmx/findLocation2?str=3513 S St NW&f=json")

but nothing is working. By the way, I had no problem making the request using Mathematica.

I will appreciate any help. Thank You in Advance.

1 个答案:

答案 0 :(得分:1)

这里发生了一些不同的事情。下面的作品:

url <- "http://citizenatlas.dc.gov/newwebservices/locationverifier.asmx/findLocation2"
body <- list(str = "3513 S St NW", f = "json")
httr::POST(url, body = body, encode = "json")

一些区别:

  • POST用于托管+端点,而不仅仅是端点
  • 不包括响应的详细信息(内容类型,长度)
  • 为正文指定的编码(根据httr::POST文档)
  • bodyencode之所以命名,是因为它们不在文档中的位置上,因此位置参数匹配不起作用