3

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))
Zizouz212
4,9985 gold badges46 silver badges66 bronze badges
asked Sep 19, 2015 at 0:25

2 Answers 2

1

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

answered Sep 19, 2015 at 3:49
Sign up to request clarification or add additional context in comments.

4 Comments

@Bilal Xenon Mellah What do you want to know?
ok there is any way after i receive the response to get that id from the page before sending the request ?
found this : r = requests.post(url, data=json.dumps(payload)) maybe json is the solution ?
@FreeSteampowresGames json won't get you past the HTTP protocol barrier. If you have access to the site, you could modify it to remember the last stored id and then your initial idea would work. Just wondering, what is the end goal of the post request. You seem to want to send data to the server that the server already knows...
1

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.

answered Sep 19, 2015 at 7:26

2 Comments

there is a way , for example : github.com/davidyen1124/Facebot get all the data needed to fill the payload with : fb_dtsg , wwwupp ,csid,tids , ids but i didn't figure it out how he does that
found this : r = requests.post(url, data=json.dumps(payload)) maybe json is the solution ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.