request: Post Request ok with postman but failed using code in python

Hi everybody,

I’m trying to write a little python code to get a json list from an website Api.

with postman i use the url and the two needed params and the result is good (as you can see in postman picture. postman

After this i take postman to generate the python Code related to this query, the result is like this

import requests

url = "https://ww5.files-seekr.com/api/search/all"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"query\"\r\n\r\nbatman\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"api_key\"\r\n\r\n1305ca510f59d2d1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'cache-control': "no-cache",
    'postman-token': "eae0d2e2-be2b-b95b-aebd-15226e17eed4"
    }

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

print(response.text)

And using python Gui, the result is not ok (error 405)

Python

i’ve tried using different syntax of the python code with no success (this following is one of them)

import requests

url = "https://ww5.files-seekr.com/api/search/all"

payload = {'api_key': '1305ca510f59d2d1', 'query': 'batman'}

response = requests.post(url=url, data=payload)

print(response.text)

Can someone give me a little help ?

Thanks a lot.

About this issue

Most upvoted comments

Wait. Which Python? This is a JavaScript library.

Thanks @GSManganneau.

This did not work

data = {'foo': {'bar': 'baz'}}
requests.put(url, header=header, data=data)

But this did

import json
data = {'foo': {'bar': 'baz'}}
requests.put(url, header=header, data=json.dumps(data))

That’s in Python 2.7, Requests 2.23.0.

Wait. Which Python? This is a JavaScript library.

No one care your comment and still think this repo for requests in Python.

Hi, In my case, I was trying to send an image to the server. The python requests library code i got from postman was incomplete. Here is python code generated by postman.

import requests url = “my_url” payload = {‘data’: ‘{“temp”: “37”, “image”: “my_photo”, “isPresent” : true}’} files = { ‘visitorPhoto’: open(‘file_name_path’,‘rb’)} # at this statement the error was happening. headers = { ‘Secretkey’: ‘ABC’, ‘Accesskey’: ‘XYZ’ } response = requests.post(url, headers=headers, data = payload, files = files) print(response.text)

It gives some error. So the problem was that the since I’m sending an ‘image’ and in the postman I’m explicitly selecting an image from local drive, but the generated code does not have ‘image’ file type info. So replace the line where the error was happening with this. files = {“visitorPhoto”: (“img.png”, open(‘image_file_path’, ‘rb’), “image/png”)} Then it will work fine. You can also do that same with http.client library code.

Hi, Try using de code from python http.client instead of python requests. I have bin at this for a long time with python requests but no luck. Now i tried

import http.client
import mimetypes
conn = http.client.HTTPSConnection("first_part-url")
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append('--' + boundary)
dataList.append('Content-Disposition: form-data; name=mXml; filename={0}'.format('9120460_202001260915 .xml'))

fileType = mimetypes.guess_type('/Users/Documents/python_xml/ filename.xml')[0] or 'application/octet-stream'
dataList.append('Content-Type: {}'.format(fileType))
dataList.append('')

with open('/Users/Documents/python_xml/ filename.xml') as f:
  dataList.append(f.read())
dataList.append('--'+boundary+'--')
dataList.append('')
body = '\r\n'.join(dataList)
payload = body
headers = {
  'Content-Type': 'multipart/form-data',
  'Accept': 'application/xml',
  'mDbg': 'J',
  'mTst': 'J',
  'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/second_part_url", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Good luck.

Hi guys, I had the same kind of issue. I don’t know why but when you give the payload as a python dict it does not work, you have to give it as a string. I hope it wil help

in my case, i had to send a zip file to the server and i was facing same issue, working in postman but not working in python. Here is my code which worked finally.

`import requests url = ‘[API URL]’

header = { ‘ApiKey’: ‘ABCD1234’, } file_z = open(‘C:\python_code\test.zip’,‘rb’) files = {“file”: (“test.zip”, file_z, ‘application/zip’)} s = requests.Session() r = s.post(url, files=files, headers=header) print(r.status_code) print(r.content)`

in my case, where in postman returns 200, and in python 405, I added user-agent to headers {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"} And python returns 200

It’s now working for me, Postman seems to covert the WebKitFormBoundary bit to numbers, when intercepting both requests with Charles. Changing this in the Python code gave a 200 response:

import requests

url = "https://api.testing.aws.xxxx.net/auth/access_token"

payload = "----------------------------578395087689527433693242\r\nContent-Disposition: form-data; name=\"client_id" \ "\"\r\n\r\nrxxxxx\r\n----------------------------578395087689527433693242\r\nContent-Disposition: form-data;" \ " name=\"grant_type\"\r\n\r\npassword\r\n----------------------------578395087689527433693242\r\nContent-Disposition:" \ " form-data; name=\"username\"\r\n\r\n1\r\n----------------------------578395087689527433693242\r\nContent-Disposition:" \ " form-data; name=\"password\"\r\n\r\n!1password!\r\n----------------------------578395087689527433693242\r\nContent-Disposition:" \ " form-data; name=\"scope\"\r\n\r\nuser.read" \ "\r\n----------------------------578395087689527433693242--" headers = { 'Accept': "application/vnd.xxxx+json;version=2.0", 'Content-Type': "multipart/form-data; boundary=--------------------------578395087689527433693242", } response = requests.request("POST", url, data=payload, headers=headers, verify=False) print(response.text)