0

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 base package. Could you advise what I'm doing wrong?

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! +++++++++++++
Vytas
  • 49
  • 1
  • 6

1 Answers1

-1

This link may have the solution to your problem : Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request?. Have a look at the cookie path adjustment part ... you may need to do so before your second HTTP Post request.

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, then sanitize those as required and post those here please? That will allow us to help you better. Thanks.

Note/Extra Reading Materials:

1)If you need help with using Firebug - here is one link: How to debug the http response headers from a HTTP call

2) Some extra info on jsessionID:

https://notes-from-a-dev.blogspot.com/2014/07/understanding-jsessionid.html

3) This link has an interesting explanation on cookie and context: https://notes-from-a-dev.blogspot.com/2014/04/sharing-jsessionid-across-applications.html 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.

John Eipe
  • 10,922
  • 24
  • 72
  • 114
Suraiya Khan
  • 102
  • 4