Python requests : trying to understand form data

Question:

i am new to requests in python and i’m trying to understand what’s the data I send in the request and what i’m getting back.

Firstly, to understand better, i used the network inspector on chrome and uploaded a file on the website i’m going to send requests to later (the ultimate goal is to upload my file with requests).
It starts by opening a modal window with parameters so i’m guessing in python in something as easy as this (in python):

url = 'myurl'
params = {'whatever params i need'}
export = s.get(url, params=params)

if i print the status_code of this i get 200 so i’m guessing until then it’s fine.
then it sends a post to the url without any parameters but with data like this (in python):

url = 'myurl'
data= {'confused'}
export = s.get(url, data=data)

here is where i’m getting a little confused. in the network inspector the data sent looks like this :

------WebKitFormBoundaryf2WTKCh05lDGbAAG
Content-Disposition: form-data; name="form[_token]"

Kmzz8c_N9qfuo8AZ1Pd1OFgaYzE9AFtitmaLkg0-y_g
------WebKitFormBoundaryf2WTKCh05lDGbAAG
Content-Disposition: form-data; name="form[importModule]"; filename="myfile.xml"
Content-Type: text/xml


------WebKitFormBoundaryf2WTKCh05lDGbAAG--

what does all this mean ? how am i supposed to write this in python ? And im guessing this "Kmzz8c_N9qfuo8AZ1Pd1OFgaYzE9AFtitmaLkg0-y_g" is the token, but how do i get in the first place too ?

thank you for your help and time !

Asked By: clemdcz

||

Answers:

You seem to be confused about "parameters" (query string parameters, "GET parameters", in any case the thing you use params= for in Requests) and form data.

What you see in the network inspector in the POST request is the form data (in particular, multipart/form-data data). If you inspect the form in the modal window, you’ll probably find a hidden field with name="form[_token]", and a file field with name="form[importModule]".

To emulate that POST (with a file upload) with Requests, you’d do something like

s.post(
   url="...",
   data={
      "form[_token]": "....",
   },
   files={
      "form[importModule]": open("some_file.xlsx", "rb"),
   },
)

To actually get the value for _token, you’d probably need to parse the response from the first GET request you do.

Answered By: AKX
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.