How to send form-data using python requests?

Viewed 16924

I'm trying to send a POST request using python 3 and the requests library. When I use postman I'm obtaining the result that I'm expecting, so I copy the code generated by Postman, and in that way it is working.

This is the code generated by postman code:

import requests

payload = "name=\"claveElector\"\r\n\r\nABCDEF01234567H400\r\nname=\"numeroEmision\"\r\n\r\n01\r\nname=\"ocr\"\r\n\r\n4158093946570\r\nname=\"g-recaptcha-response\"\r\n\r\nsome-captcha\r\nname=\"modelo\"\r\n\r\na"

url = "https://listanominal.ine.mx/scpln/resultado.html"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"claveElector\"\r\n\r\nTPRSJC95010209H400\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"numeroEmision\"\r\n\r\n01\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ocr\"\r\n\r\n4158093946570\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"g-recaptcha-response\"\r\n\r\nsome-re-captcha\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"modelo\"\r\n\r\na\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

To be more clear in the part of why is not working is the difference that exists in both results.

With the postman code i got this

That code is generated with the following data: That code is generated with the following data

So I tried to do the same using my own code, I've tried to send the data in the file part, and in the data part but is not working. Reading other StackOverflow questions, they suggest to use the MultipartEncoder that is part of the Requests Toolbelt library.

So my implementation ended up like this:

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

clave_elector = "ABCDEF01234567H400"
numero_emision = "01"
ocr = "1234567846570"
modelo = "a"

params = {
    "claveElector": clave_elector,
    "numeroEmision": numero_emision,
    "ocr": ocr,
    "modelo": modelo,
    "g-recaptcha-response": ''
}

data = MultipartEncoder(fields = params)

headers = {
    'Content-type': data.content_type
}
response = requests.post(
    'https://listanominal.ine.mx/scpln/resultado.html',
    data = data,
    headers = headers
)
print(response.text)

With the custom code I got the following

If you try both codes, you can see how the results are different. And I'm interested in the first one, the one that is got with the code that was generated by Postman.

I don't know why my code is not working, what I am doing wrong? What I'm trying to say is that somehow with my code the request is not being send properly so the website is not able to read it.

1 Answers

After so much try and error, the only way that I found to achieve this, was to use the following code:

from requests import Request, Session

clave_elector = "TPRSJC95010209H400"
numero_emision = "01"
ocr = "4158093946570"
modelo = "a"

data = {
    "claveElector": clave_elector,
    "numeroEmision": numero_emision,
    "ocr": ocr,
    "modelo": modelo,
    "g-recaptcha-response": 'hjkkjgh'
}

request = Request(
    'POST', 
    'https://listanominal.ine.mx/scpln/resultado.html',
    files = {
        'claveElector': (None, data['claveElector']),
        'numeroEmision': (None, data['numeroEmision']),
        'ocr': (None, data['ocr']),
        'modelo': (None, data['modelo']),
        'g-recaptcha-response': (None, 'xxx'),
    }
).prepare()


s = Session()
response = s.send(request)

print(response.text)

I really don't know why this was different from what I was trying to do, but it works. If someone could comment why this works I'll really appreciate.

Related