Title: Post method with Requests
Tags: python;python-requests
Question: 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))
```


Here is the accepted answer: 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

Comment for this answer: ok there is any way after i receive the response to get that id from the page before sending the request ?
Comment for this answer: found this :  `r = requests.post(url, data=json.dumps(payload))`
maybe json is the solution ?
Comment for this answer: @Bilal Xenon Mellah What do you want to know?
Comment for this answer: @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...

Here is another answer: 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.

Comment for this answer: there is a way , for example : 
https://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
Comment for this answer: found this :  `r = requests.post(url, data=json.dumps(payload))`
maybe json is the solution ?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:09/19/15', 'FavoriteCount': 3.0, 'PostTypeId': 1, 'Tags': '<python><python-requests>', 'Title': 'Post method with Requests', 'ViewCount': 453, 'id': 32662854, 'user_AboutMe': None, 'user_AccountId': 5245771.0, 'user_DisplayName': 'FreeSteampowresGames', 'user_Reputation': 293.0, 'user_Views': 39.0, 'user_id': 4191559.0}}