Thiago
Thiago

Reputation: 724

Include session in HTTP POST header

I need to upload a JSON to an API using Python. I have a session header to add to the POST request for authorizing the request. The session is the following format:

session=value=

I have been trying to add it as a header in the python urllib2 library however the API is still giving me a 401 Unauthorized. So far, I'm doing like this:

req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')
req.add_header('Set-Cookie', 'session=value=')
response = urllib2.urlopen(req, data=json.dump(data))

I have been reading the urllib2 documentation and also the http cookies and I couldn't find a clear way to send this cookie through the headers. Could anybody please give a light here on how to do it? Unfortunately, for privacy issues, I cannot show the url and the cookie.

Thanks in advance.

Upvotes: 2

Views: 2412

Answers (1)

Ali Nikneshan
Ali Nikneshan

Reputation: 3502

You Should use Cookie not Set-Cokkie. Others are fine

req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')
req.add_header('Cookie', 'session=value=')
response = urllib2.urlopen(req, data=json.dump(data))

When Server response, it uses Set-Cookie to persist a cookie in client, when client want to callback, it just use Cookie as header.

Upvotes: 1

Related Questions