抑制302 httr POST返回的错误 [英] Suppressing 302 Error returned by httr POST

查看:673
本文介绍了抑制302 httr POST返回的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R httr POST函数向jSON发送一个API。 API正确地返回一个302:Found消息,但httr正在退出该函数,然后我才能抓住响应的主体(这是一个jSON主体,有一些关键的信息)。



使用Verbose()参数运行httr,以下是响应:

  ;  -  HTTP / 1.1 302 Found 
< - Cache-Control:no-cache
< - Pragma:no-cache
< - Content-Length:47
< ; - Content-Type:application / json; charset = utf-8
< - Expires:-1
函数中出现错误(类型,msg,asError = TRUE):
不能进行必要的数据回退

我已经从终端运行相同的cURL文章,并且可以确认我发送的是从API产生回复302和所需的身体。



为了参考,我的R代码如下。 (注意:y是jSON格式的主体)

  POST(https:// thewebsite,authenticate(myusername mypassword,type =basic),
add_headers(Content-Type=application / json),
body = y,verbose())



有关如何绕过错误并捕获302消息内容的任何想法?

解决方案

我刚刚花了一些时间与这个问题自己。问题出在HTTP规范(这基本上是RCurl坚持)和什么浏览器实际上做的不同。



事件的顺序是这样:


  1. 您向服务器发出POST请求

  2. 服务器处理请求并将您重定向到新网址

  3. RCurl将此类似于像POST这样的新请求对待,并尝试重放正文。 (浏览器不尝试重新发送数据)

  4. 它不能重新发送数据,因为底层的RCurl不是这样构造的,这是可能的(这就是为什么curl抱怨: 必要的数据倒转不可能)

解决方案很简单 - 禁用以下重定向与 config(followlocation = 0L)

  POST(https:// thewebsite,
authenticate(myusername,mypassword),
content_type_json(),
config(followlocation = 0L),
body = y,

b $ b#PS with httr 0.4你可以简化为
POST(https:// thewebsite,
authenticate(myusername,mypassword),
config(followlocation = 0L ),
body = x,encode =json

然后需要查看位置字段的内容,并自己执行重定向。



有关基本问题的更多讨论,请参阅:




I'm using the R httr POST function to send a body of jSON to an API. The API is, properly, returning a 302: Found message, but httr is exiting the function before I'm able to grab the body of response (which is a jSON body, with some key bits of info.)

Running httr with the Verbose() argument, the following is the response:

<- HTTP/1.1 302 Found
<- Cache-Control: no-cache
<- Pragma: no-cache
<- Content-Length: 47
<- Content-Type: application/json; charset=utf-8
<- Expires: -1
Error in function (type, msg, asError = TRUE)  : 
  necessary data rewind wasn't possible

I've run the same cURL post from terminal, and can confirm what I'm sending produces a reply from the API with both the 302 and the desired body.

For reference my R code follows. (note: y is the jSON formatted body)

POST("https://thewebsite",authenticate("myusername","mypassword",type="basic"),
    add_headers("Content-Type" = "application/json"),
    body = y, verbose())

Any thoughts on how to bypass the error and capture the 302 message content?

解决方案

I just spent some time battling with this issue myself. The problem comes down to a difference in the HTTP spec (which is basically what RCurl adheres to) and what browsers actually do.

The sequence of events is this:

  1. You issue POST request to server
  2. Server handles request and issues you a redirect to a new url
  3. RCurl treats this like new request like a POST, and tries to replay the body. (Browser don't try and resend the data)
  4. It can't resend the data because the underlying RCurl isn't constructed in such a way this is possible (that's why curl complains: "necessary data rewind wasn't possible")

The solution is simple - disable the following of redirects with config(followlocation = 0L):

POST("https://thewebsite",
  authenticate("myusername","mypassword"),
  content_type_json(),
  config(followlocation = 0L),
  body = y, 
)

# PS with httr 0.4 you can simplify to
POST("https://thewebsite",
  authenticate("myusername","mypassword"),
  config(followlocation = 0L),
  body = x, encode = "json" 
)

You'll then need to look at the contents of the location field, and do the redirect yourself.

For more discussion of the underlying issue, see:

这篇关于抑制302 httr POST返回的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆