6

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.

Jacobo
  • 1,259
  • 2
  • 19
  • 43
  • 1
    "is not working" - how _exactly_ is it not working? "If you try both codes" - please include the relevant output in the question – ForceBru Jun 23 '19 at 21:24
  • Possible duplicate of [Python : Trying to POST form using requests](https://stackoverflow.com/questions/20759981/python-trying-to-post-form-using-requests) – sal Jun 23 '19 at 21:25
  • So it is okay to put the entire html code? It’s a lot of code. – Jacobo Jun 23 '19 at 21:26
  • @JacoboTapia your code generate the same result for both posted codes – Ghassen Jun 23 '19 at 21:38
  • @Ghassen read carefully the code, I updated the code that resulted from both requests. I deleted the header and duplicated stuff, but It is different. If you inspect it carefully It is different. – Jacobo Jun 23 '19 at 21:42
  • It always return a page. But the correct one is only fetched when I use the postman generated code. With my code it returns me like an error page. – Jacobo Jun 23 '19 at 21:51

1 Answers1

8

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.

Jacobo
  • 1,259
  • 2
  • 19
  • 43
  • [possible related source link](https://2.python-requests.org/en/master/_modules/requests/models/#PreparedRequest.prepare) – XoXo May 15 '22 at 01:30
  • Related docs on requests.post() for form data: https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file – Samuel O.D. May 26 '23 at 09:38