FreeSteampowresGames
FreeSteampowresGames

Reputation: 303

Post method with Requests

I'm trying to make a simple post method with the requests module, like this :

 s=requests.Session() 

 s.post(link,data=payload)

In order to do it properly, the payload is an id from the page itself, and it's generated in every access to the page.

So I need to get the data from the page and then proceed the request.

The problem when you accessed the page is that a new id will be generated.

So if we do this:

 s=requests.Session() 

 payload=get_payload(s.get(link).text)

 s.post(link,data=payload)

It will not work because when you acceded the page with s.get the right id is generated, but when you go for the post request, a new id will be generated so you'll be using an old one.

Is there any way to get the data from the page right before the post request?

Something like:

 s.post(link,data=get_data(s.get(link))

Upvotes: 3

Views: 507

Answers (2)

lvc
lvc

Reputation: 35059

In general, there is no way to do this. The server's response is potentially affected by the data you send, so it can't be available before you have sent the data. To persist this kind of information across requests, the server would usually set a cookie for you to send with each subsequent request - but using a requests.Session will handle that for you automatically. It is possible that you need to set the cookie yourself based on the first response, but cookies are a key/value pair, and you only appear to have the value. To find the key, and more generally to find out if this is what the server expects you to do, requires specific knowledge of the site you are working with - if this is a documented API, the documentation would be a good place to start. Otherwise you might need to look at what the website itself does - most browsers allow you to look at the cookies that are set for that site, and some (possibly via extensions) will let you look through the HTTP headers that are sent and received.

Upvotes: 1

Elipzer
Elipzer

Reputation: 911

When you do a post (or get) request, the page will generate another id and send it back to you. There is no way of sending data to the page while it is being generated because you need to receive a response first to process the data on the page and once you have received the response, the server will create a new id for you the next time you view the page.

See https://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/images/HTTP.png for a simple example image of a HTTP Request

Upvotes: 1

Related Questions