0

I am trying to post a request on a website that requires credentials. I have used postman and sync with the browser using interceptor (google extension) to make use of the cookies stored correctly in postman. I have succeeded in posting the request in postman and here is the settings I did. enter image description here

When trying to copy the code of the Python requests package, I got an invalid response 404 and something went wrong. Here's the python code I got from Postman (not working)

import requests

url = "https://eservices.moj.gov.kw/execFileUpload2"

payload = {'selectAser': '211053300',
'phoneNo': '55075052',
'selectAsed': '120987470-1',
'comReg': '',
'bankType': '1850',
'attachments': '(binary)',
'searchType': '0745'}
files=[

]
headers = {
  'Cookie': 'BNES_JSESSIONID=FBVxGgtjjJhDGmcnG/XOglUeuJlG/mwpJH9+ICQw/bL3/2Y383HWR4f+WzRXsJBG5jHf33qCsgMBOHLeTKxJRBwlkrkKE1tGbdywVgdikWhC4VcCUd1yIIYJBFm4m/60lfkGqvgk+UfaMbPKe3CUVRvsTaMCLn+A; BNIS___utm_is1=834onz3cMbm6+rxfXql7Q9R5wUqKP9h0Og9waF061RytqqFpbicKoodJVczfRblgLKkNejO9xULfy/gV59QKOKqOE5nH62HC/3yuYJehn8hT5JL0Trtl1Q==; BNIS___utm_is2=h3TrzcqVULgLWXXvNz3L2bQtrG+U/tHgaCBTF5MLDA6zaHEpzzwCy9Rj99RJySZUYmO8bQuFJ6s=; BNIS___utm_is3=xl2RfksMuxdJJrgDWe3RlTXaGAMcCXNp8jk39UeD37JWegODHQm0CVYCakYal9jUfWovaY2FrKriBIFNQOFbyIxcgMpHKnERXJrFCsvLKrmZNPgeZdMezA==; BNIS_vid=dVdYD1HKY4P0pa5whnuuvLgo71u2130LdG9wpcgfK0yYw3B1mLAfK2pzLr7+xPH1h3PVudRNDGJQHyVY0Y9fVfhte25qE3ArdMOHRB4jcyMGq/9iCsU5goBa1Q2hVs/8Fqd53PjKs7hT81XeYQ8Jtani11zEpPgkw70aLejHmSSykk8nACMnKMr0zP4RqoujmqEbAHE3+e6Odf2XSkBCWh3jPiyUqWmU4zYGail1N6A=; JSESSIONID=6ZLK74MAMrYOWAb39OQ7EYLHtt9GjRFzwvgwACd9idbb76Fe7q4p!669384774'
}

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

print(response.text)

This is the response I got

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html dir="rtl">
 <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
        <title>::بوابة العدل الالكترونية::</title>
        <link type="text/css" rel="stylesheet" href="/Styles/NMaster.css" />

        <style type="text/css">
                <!--
                        H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
                        PRE, TT {border: 1px dotted #525D76}
                        A {color : black;}A.name {color : black;}
                -->
        </style>
 </head>
 <body>
     <h1 style="text-align:center;">حدث خطأ ما </h1>

 </body>
</html>

Here are some snapshots (may help us) enter image description here

enter image description here

enter image description here

MendelG
  • 14,885
  • 4
  • 25
  • 52
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • Welp, the status code and message doesn't mean anything in this I guess. Can it be because you are trying to register twice? – doneforaiur Aug 06 '23 at 13:25
  • But in postman I have clicked on Send button several times and got the same response `Registered Successully` – YasserKhalil Aug 06 '23 at 13:27
  • I tried sending a request but can't establish a connection. Could you try with `allow_redirects=True` in the `request` function, since it redirects... – doneforaiur Aug 06 '23 at 13:31
  • 2
    @YasserKhalil Use a tool such as https://curlconverter.com/ and see if it works. To get the curl - see: https://stackoverflow.com/questions/49432735/converting-a-postman-request-to-curl – MendelG Aug 06 '23 at 13:33
  • @MendelG From Postman to cURL to Python. Hmm. In the past Insomnia gave me similar problems, this approach may work. – doneforaiur Aug 06 '23 at 13:35

2 Answers2

2

It's challenging to provide a precise solution without examining the file you're working with.

If your request functions correctly in Postman but encounters issues in Python, it's likely that there's a discrepancy in your Python code. A handy approach to troubleshooting this would be to:

  1. Obtain the CURL command from Postman. See: Converting a Postman request to cURL.

  2. Once you have the CURL command, you can then convert it into Python code using a tool like curlconverter.com.

By comparing the converted Python code with your existing Python code, you should be able to pinpoint the problem

MendelG
  • 14,885
  • 4
  • 25
  • 52
  • Thanks a lot. I have copied the curl from developer tools as bash and paste in the website recommended for me and it converted to python `https://curlconverter.com/` – YasserKhalil Aug 06 '23 at 13:49
  • 1
    @YasserKhalil ...And did it solve the issue? – MendelG Aug 06 '23 at 13:50
  • Yes, I have put the code in my question as an update. But need more help. – YasserKhalil Aug 06 '23 at 13:52
  • 2
    @YasserKhalil You are now editing your question as a _new_ one. Consider marking this question [as closed](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) and open a _new_ one. I'm thinking to rollback your question to the previous version, as your asking a new question – MendelG Aug 06 '23 at 13:54
  • I've now rollbacked your question.. – MendelG Aug 06 '23 at 14:08
1

code looks correct, but there are several things that you can try with your code.

  1. try using sessions

  2. instead of data you can try with json parameter in post method.

     import requests
     sess = requests.Session()
     url = "https://eservices.moj.gov.kw/execFileUpload2"
     payload = {'selectAser': '211053300',
      'phoneNo': '55075052',
      'selectAsed': '120987470-1',
      'comReg': '',
      'bankType': '1850',
      'attachments': '(binary)',
      'searchType': '0745'}
    
    files=[]
    headers = {
      #mention a proper header according to website
    }
    
    response = sess.post(url, headers=headers, data=payload,
     files=files)
    #Or you can try below one also whichever works for you
    response = sess.post(url, headers=headers, json=payload, files=files
    
    print(response.text)