2

I recently asked a question involving a put request and the solution was to update the url to be https:// instead of http:// to avoid a redirect wiping out the payload.

Specifically, when my code sent out the first put request, it got a response of HTTP/1.0 301 Moved Permanently, and in the response header was the correct link. My code then sent out a second put request to the new link, but this request didn't include my payload. Below is what I saw in the terminal.

send: 'PUT /api/data/v8.0/contacts(0000000-0000-0000-0000-000000000001)/firstname HTTP/1.1\r\nHost: example.crm.dynamics.com\r\nContent-Length: 25\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.9.1\r\nConnection: keep-alive\r\nContent-Type: application/json\r\nAuthorization: Bearer sampleauthorizationtoken\r\n\r\n{"value": "WebApiUpdate"}'
reply: 'HTTP/1.0 301 Moved Permanently\r\n'
header: Location: https://example.crm.dynamics.com/api/data/v8.0/contacts(0000000-0000-0000-0000-000000000001)/firstname
send: 'PUT /api/data/v8.0/contacts(0000000-0000-0000-0000-000000000001)/firstname HTTP/1.1\r\nHost: example.crm.dynamics.com\r\nContent-Length: 0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.9.1\r\nConnection: keep-alive\r\nContent-Type: application/json\r\nAuthorization: Bearer sampleauthorizationtoken\r\n\r\n'
reply: 'HTTP/1.1 204 No Content\r\n'

If you scroll all the way to the right, you should see that the top put request has data attached while the bottom doesn't. Why would this happen?

Below is the code used to generate the request.

payload = {"value" : "WebApiUpdate"}
url = "http://example.crm.dynamics.com/api/data/v8.0/contacts(0000000-0000-0000-0000-000000000001)/firstname"
headers = {"Authorization":"Bearer "+token_response['accessToken']}
r = requests.put(url,json=payload,headers=headers)
Community
  • 1
  • 1
mucle6
  • 645
  • 1
  • 10
  • 24
  • Just a wild guess... I am wondering if `requests` is creating a new cookie for the redirect request, and so the auth doesn't carry over. See if using a [`requests.session` object](http://docs.python-requests.org/en/latest/user/advanced/#session-objects) rather than a normal request object fixes this. – dagrha Apr 07 '16 at 18:07

0 Answers0