I'm using python-requests module to handle oAuth request and response.
I want to set received access_token (response content as dict) in requests.session.cookies object.
How can I update existing cookies of session with received response from server?
[EDIT]
self.session = requests.session(auth=self.auth_params)
resp = self.session.post(url, data=data, headers=self.headers)
content = resp.content
I want to do something like:
requests.utils.dict_from_cookiejar(self.session.cookies).update(content)
Here, requests.utils.dict_from_cookiejar(self.session.cookies) returns dict with one session key. Now, I want to update received response content in self.session.cookies.
requests can do that for you, provided you tell it all the requests you make are part of the same session:
>>> import requests
>>> s = requests.session()
>>> s.get('https://www.google.com')
<Response [200]>
>>> s.cookies
<<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='NID'...
Subsequent requests made using s.get or s.post will re-use and update the cookies the server sent back to the client.
To add a Cookie on your own to a single request, you would simply add it via the cookies parameter.
>>> s.get('https://www.google.com', cookies = {'cookieKey':'cookieValue'})
Unless the server sends back a new value for the provided cookie, the session will not retain the provided cookie.
In order to provide a cookie yourself to the requests module you can use the cookies parameter for a single request and give it a cookie jar or dict like object containing the cookie(s).
>>> import requests
>>> requests.get('https://www.example.com', cookies {'cookieKey':'cookieValue'})
But if you want to retain the provided cookie without having to set the cookies parameter everytime, you can use a reqests session which you can also pass to other funtions so they can use and update the same cookies:
>>> session = requests.session()
>>> session.cookies.set('cookieKey', 'cookieName')
# In order to avoid cookie collisions
# and to only send cookies to the domain / path they belong to
# you have to provide these detail via additional parameters
>>> session.cookies.set('cookieKey', 'cookieName', path='/', domain='www.example.com')
This code worked for me. hope it can help to someone else.
I want to update session.cookies variable with received response values from post request.
so, same request value can be used in another post/get request.
here, what I did:
1) updated requests module to 1.0.3 version.
2) created 2 functions
session = requests.session()
def set_SC(cookie_val):
for k,v in cookie_dict.iteritems():
if not isinstance(v, str):
cookie_dict[k] = str(v)
requests.utils.add_dict_to_cookiejar(session.cookies,
cookie_val)
def get_SC():
return requests.utils.dict_from_cookiejar(session.cookies)
In another function:
setSC(response.content)
Related
I am trying to get some data from an xml of a website which requires to login first. I try to get python requests Session and I am able to login using get method (shows 200). After that I try to get access to the xml, but it gets me 401.
So far I know that the server checks with each call whether the client is sending a JSESSIONID cookie and whether the VALUE matches the current session.
I manage to get the corresponding cookies and tried to send it via post method but still get 401.
Maybe I think to complicated. I also do not need to achieve this with requests. I would be glad to just get the information from the xml.
import requests
login_url = 'https://www.example.com/login
USER_NAME = 'user'
PASSWORD = 'pass'
xml = 'https://www.example.com/channel_index?123&p=web'
with requests.Session() as s:
response = s.get(login_url,auth = (USER_NAME, PASSWORD))
print(response)
r = s.get(xml)
cookies = s.cookies.get_dict()
r = s.post(xml, cookies = cookies)
print(r)
This works fine, I can get data returned:
r = urllib2.Request("http://myServer.com:12345/myAction")
data = json.dumps(q) #q is a python dict
r.add_data(data)
r=urllib2.urlopen(r)
But doing the same with requests package fails:
r=requests.get("http://myServer.com:12345/myAction", data=q)
r.text #This will return a message that says method is not allowed.
It works if I make it a post request: r=requests.post("http://myServer.com:12345/myAction", data=json.dumps(q))
But why?
According to the urllib2.urlopen documentation:
the HTTP request will be a POST instead of a GET when the data parameter is provided.
This way, r=urllib2.urlopen(r) is also making a POST request. That is why your requests.get does not work, but requests.post does.
Set up a session
import session
session = requests.Session()
r = session.get("http://myServer.com:12345/myAction", data=q)
print r.content (<- or could us r.raw)
I'm trying to send a post request to a website to get a json response. I can see the json response in Chrome Inspector when I click on a link, but I can get it using requests.
Firstly I tried to used requests Session to get the cookies first and use them in the post request, to no avail.
session = requests.Session()
session.get('http://www.auchandrive.fr/drive/pagestatique.pagetemplate.popuphandler.popinchangementmagasin.changermag/537?t:ac=PAGE_STATIQUE_ENGAGEMENTS')
response = session.post('http://www.auchandrive.fr/drive/rayon.productlist.pagination_0.topage/1?t:ac=3686973/3686997')
print response.text
Secondly I used Selenium+PhantomJS to get the cookies and used them in requests, no results!
browser = webdriver.PhantomJS(PHANTOMJS_PATH)
browser.get('http://www.auchandrive.fr/drive/pagestatique.pagetemplate.popuphandler.popinchangementmagasin.changermag/537?t:ac=PAGE_STATIQUE_ENGAGEMENTS')
all_cookie = {}
for cookie in browser.get_cookies():
all_cookie[cookie['name']] = cookie['value']
rep = requests.post('http://www.auchandrive.fr/drive/rayon.productlist.pagination_0.topage/1?t:ac=3686973/3686997', cookies=all_cookie)
It only works when I manually take the cookies from Chrome.
I can't see what's the problem!
session = requests.Session()
session.get('http://www.auchandrive.fr/drive/pagestatique.pagetemplate.popuphandler.popinchangementmagasin.changermag/537?t:ac=PAGE_STATIQUE_ENGAGEMENTS')
response = session.post('http://www.auchandrive.fr/drive/rayon.productlist.pagination_0.topage/1?t:ac=3686973/3686997')
print(response.json)
Using the json attribute will fetch the JSON response. You can also use requests to make a persistent session, so the cookies are provided.
response.cookies #The cookies attribute
When I use PHP to get cookie, it returns:
session_id=abih14s7l4lgo3splta7f6bd14; cccaa78fa9e13785130119a4924db0f4=96637ae... (more)
But when I use Python, it returns:
session_id=abih14s7l4lgo3splta7f6bd14
... the rest of cookies is lost.
My code Python:
res_post = requests.post(LOGIN_URL, data = {mydata})
cookies = dict(res_post.cookies.items())
Looks like you're using the requests library. The response.cookies is already a dictionary, so there's no need to cast it again.
You should access the cookies on the response like so:
response = requests.post(LOGIN_URL, data={mydata})
print response.cookies['session_id']
See here for more detail: http://docs.python-requests.org/en/latest/user/quickstart/#cookies
in Python Requests
How to get cookies values after authentication, the server must gave you a valid cookies after the authentication, in my case I can't see the cookies, even if I tried to look in .cookies or .headers
r = requests.get(url)
print r.cookies
the response contains an attribute cookies