Search code examples
pythonrcurlhttr

R httr error 403


I have a code in python working which uploads a file to server with following code

 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)

In R I am using the following code as it's replacement

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

But it is throwing out error

HTTP/1.1 403 Forbidden

On requestbin the requests are received as

For 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"}

For 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"}

Any suggestions?


Solution

  • Hard to say exactly what the server is complaining about, but if we compare the two requests, we see there is a difference in content type and user-agent.

    You can suppress the content type with

    upload_file(path = file_path, type="")
    

    You can suppress the user agent by also passing

    user_agent("")
    

    as a parameter to PUT()