R httr错误403

时间:2017-03-28 20:33:57

标签: python r curl httr

我在python工作中有一个代码,它使用以下代码将文件上传到服务器

 with open(file_path, 'rb') as fp:
     r = requests.Request('PUT', signedRequest, data=fp.read())
     prepped = r.prepare()
     s = requests.Session()
     resp = s.send(prepped)

在R中我使用以下代码作为替换

PUT(url = signedRequest,
    body = upload_file(path = file_path),verbose()
   )

但它丢掉了错误

HTTP/1.1 403 Forbidden

在requestbin上,请求以

的形式收到

对于Python

Cf-Connecting-Ip: ****
Host: requestb.in
Connect-Time: 1
Total-Route-Time: 0
Via: 1.1 vegur
Content-Length: 6481350
Connection: close
Cf-Ipcountry: US
X-Request-Id: f9b165cc-0f42-4eaa-8b40-2eb37a6ff1ca
Accept-Encoding: gzip
Cf-Ray: 346d734353e871df-ORD
Cf-Visitor: {"scheme":"http"}

对于R

Cf-Connecting-Ip: ****
Content-Length: 6481350
User-Agent: libcurl/7.47.0 r-curl/2.3 httr/1.2.1
Total-Route-Time: 0
Via: 1.1 vegur
Connection: close
Cf-Ipcountry: US
Content-Type: text/csv
X-Request-Id: 0bfbf6ab-4658-4650-bd4d-e1e19ffdba91
Accept: application/json, text/xml, application/xml, */*
Connect-Time: 0
Accept-Encoding: gzip
Host: requestb.in
Cf-Ray: 346d6e90d70f54ec-ORD
Cf-Visitor: {"scheme":"http"}

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

很难确切地说服务器正在抱怨什么,但如果我们比较这两个请求,我们会发现内容类型和用户代理存在差异。

您可以使用

取消内容类型
upload_file(path = file_path, type="")

您也可以通过传递

来禁止用户代理
user_agent("")

作为PUT()

的参数