Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中使用POST检索响应_R_Api_Post_Web Scraping_Httr - Fatal编程技术网

如何在R中使用POST检索响应

如何在R中使用POST检索响应,r,api,post,web-scraping,httr,R,Api,Post,Web Scraping,Httr,如果您访问,您将看到搜索框 我希望输入'905/8 Ronayne St',输出'12343197398' 我正在使用R,并尝试这样做,但没有工作 post <- POST("https://www.aucklandcouncil.govt.nz/_vti_bin/ACWeb/ACservices.svc/GetMatchingPropertyAddresses", body = list('ResultCount' = "10", 'SearchText' =

如果您访问,您将看到搜索框

我希望输入'905/8 Ronayne St',输出'12343197398'

我正在使用R,并尝试这样做,但没有工作

post <- POST("https://www.aucklandcouncil.govt.nz/_vti_bin/ACWeb/ACservices.svc/GetMatchingPropertyAddresses", 
             body = list('ResultCount' = "10", 'SearchText' = "905/8 Ronayne St", 'RateKeyRequired' = "false"))

content(post, "text")

post由于发送方式的原因,只需在R中提供正确的头

R:

library(httr)

headers = c('Content-Type' = 'application/json; charset=UTF-8')
data = '{"ResultCount":"10","SearchText":"905/8 Ronayne St","RateKeyRequired":"false"}'
r <- httr::POST(url = 'https://www.aucklandcouncil.govt.nz/_vti_bin/ACWeb/ACservices.svc/GetMatchingPropertyAddresses', httr::add_headers(.headers=headers), body = data)

print(content(r)[[1]]$ACRateAccountKey)
import requests

data = {"ResultCount":"10","SearchText":"905/8 Ronayne St","RateKeyRequired":"false"}    
r = requests.post('https://www.aucklandcouncil.govt.nz/_vti_bin/ACWeb/ACservices.svc/GetMatchingPropertyAddresses', json=data).json()
print(r[0]['ACRateAccountKey'])

即使更改SearchText,POST调用也会返回400。POST调用中的某些内容违反了请求参数,在没有任何文档的情况下,很难确定错误所在。再次感谢QHarr!非常感谢:)当我在R中使用POST函数时,是否总是需要添加标题?还有,为什么我只需要为标题指定“内容类型”?(不是‘缓存控制’、‘内容编码’等?)这是关于服务器配置的预期。此外,在本例中,特别是关于您如何发送服务器需要知道的正文,以便正确处理。嗨,QHarr,我再次需要您的帮助。如果您能同时给出R和Py的答案,我们将不胜感激:)