Restful API - passing cookie to subsequent POST requests
<p>The application I want to query requires Json format. Supported method is POST. I seemingly cannot find a good example of how to get a cookie from 1 query and pass it to another query (or make subsequent queries use it as part of a <code>base package</code>. Could you advise what I'm doing wrong?</p>

<pre><code>import json
import requests
headers = {'Content-type': 'application/json'}
data = {"username":"user1", "password":"pass1"}
login_info = json.dumps(data)

session = requests.Session()

login_url = 'https://ip/login'
response = session.post(login_url, data=login_info, headers=headers, verify=False)
print session.cookies.get_dict()
#returns the following format- {'JSESSIONID': 'DE1EE0006D53EABFA4EE0C6A50D1386A'}

query_url = 'https://ip/query'
response = session.post(query_url, cookies=session.cookies.get_dict(), headers=headers, verify=False)

print response.text
#retuns  +++++++++++++++  JSESSIONID is empty! +++++++++++++
</code></pre>

<p>This link may have the solution to your problem : <a href="http://stackoverflow.com/questions/4166129/apache-httpclient-4-0-3-how-do-i-set-cookie-with-sessionid-for-post-request">Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request</a>. <strong>Have a look at the cookie path adjustment part ... you may need to do so before your second HTTP Post request.</strong></p>

<p><em>Can you capture the HTTP request, response (as for example using Firebug or Chrome) while (1) manually trying the login and query pages and (2) doing the same with your script</em>, then sanitize those as required and post those here please? That will allow us to help you better. Thanks. </p>

<p>Note/Extra Reading Materials: </p>

<p>1)If you need help with using Firebug - here is one link: <a href="http://stackoverflow.com/questions/5374100/how-to-debug-the-http-response-headers-from-a-http-call">How to debug the http response headers from a HTTP call</a> </p>

<p>2) Some extra info on jsessionID: </p>

<p><a href="http://www.cs-repository.info/2014/07/understanding-jsessionid.html" rel="nofollow">http://www.cs-repository.info/2014/07/understanding-jsessionid.html</a> </p>

<p>3) This link has an interesting explanation on cookie and context: <a href="http://www.cs-repository.info/2014/04/sharing-jsessionid-across-applications.html" rel="nofollow">http://www.cs-repository.info/2014/04/sharing-jsessionid-across-applications.html</a>
It may be clarifying the real reason behind the problem that you are facing (though the server side solution is not applicable to you). Please search for the text "AppOne/one.jsp" and read from that point onward to be able to quickly finish reading.</p>

python jsessionid