I was able to use postman and do a post request with image and a string parameter. I am not able to do the same if I copy the python code from postman and run it.
import requests
url = "yyyyyyyyyy"
querystring = {"param1":"xxxxx"}
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"queryFile\"; filename=\"file.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Authorization': "Bearer yyyyyyyyyyy",
'cache-control': "no-cache",
'Postman-Token': "fffffffffff"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
{"message":"EMPTY_FILE_NOT_ALLOWED","status":400}
You need to pass a reference to the files you want to upload via the files parameter. See python requests file upload.
Related
import requests
url = "https://apiexample.com/load/v1/action/aaaaaaaaaaaaa"
payload={}
headers = {
'Authorization': 'OAuth oauth_consumer_key="aaaaaa",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1664837015",oauth_nonce="mKyTFn7OtsV",oauth_version="1.0",oauth_signature="aaaaaaaaaaaa"',
'Cookie': 'JSESSIONID=M7n-S-aGe8asRnTjNOUGowak5i5avsRBx6A4H8au.madsedepre'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
I am trying to retrieve information from an api, and I am using postman...
How can I know what postman is doing to generate that cookie??
and how can I generate it using python requests?
I have a script that calls a POST endpoint but getting a 400 error. Meanwhile, the corresponding cURL request is successful.
First, here is the cURL:
curl -X 'POST' \
'http://localhost:8080/api/predict?Key=123testkey' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=#156ac81cde4b3f22faa4055b53867f38.jpg;type=image/jpeg'
And translated to requests:
import requests
url = 'http://localhost:8080/api/predict?Key=123testkey'
headers = {
'accept': 'application/json',
'Content-Type': 'multipart/form-data',
}
params = {'Key' : '123testkey'}
files = {'image': open('156ac81cde4b3f22faa4055b53867f38.jpg', 'rb')}
response = requests.post(url, files=files, params=params, headers=headers)
Have also tried using a URL that does not include the key, since the key is already specified in params:
import requests
url = 'http://localhost:8080/api/predict'
headers = {
'accept': 'application/json',
'Content-Type': 'multipart/form-data',
}
params = {'Key' : '123testkey'}
files = {'image': open('156ac81cde4b3f22faa4055b53867f38.jpg', 'rb')}
response = requests.post(url, files=files, params=params, headers=headers)
I thought this should be simple but I consistently get the 400 error with requests no matter what I try. Any suggestions?
Edit: have also tried 'image/jpeg' instead of 'image' to no avail.
Edit: replacing the "image" key with "file" unfortunately didn't work either
Edit: It works in postman desktop just fine, and generates the following code. However, this code also throws an error.
The generated code from postman:
import requests
url = "http://localhost:8080/api/predict?Key=123test"
payload={}
files=[
('file',('images19.jpg',open('156ac81cde4b3f22faa4055b53867f38.jpg','rb'),'image/jpeg'))
]
headers = {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
And the error from the previously generated code from postman:
{"detail":"There was an error parsing the body"}
Any help figuring out what is going on would be much appreciated!
Your issue is in the variable files you need to add with the key 'file' instead of 'image' that's the difference between your curl and your python code, also remove the header because when you pass the file parameter the request set the proper header for send files. for example:
import requests
url = 'http://localhost:8080/api/predict?Key=123testkey'
params = {'Key' : '123testkey'}
files = {'file': open('156ac81cde4b3f22faa4055b53867f38.jpg', 'rb')}
response = requests.post(url, files=files, params=params)
I'm trying to make a post request to an API endpoint with python and requests.
The endpoint requires a token. I get the token from the endpoint just fine.
When making a post request to the second endpoint Validation Error stating that body is empty.
import requests
url = "https://authz.dinero.dk/dineroapi/oauth/token"
payload = 'grant_type=password&username=****&password=****'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ****'
}
response = requests.request("POST", url, headers=headers, data = payload)
r =response.json()
token = r['access_token']
url = "https://api.dinero.dk/v1/257403/contacts"
payload = {}
payload["Name"] = "Test Name"
payload["CountryKey"] = "DK"
payload["IsPerson"] = "true"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
response = requests.post(url, headers=headers, data = payload)
print(response.text)
This is the error I get:
{"code":42,"message":"Validation Error","validationErrors":{"Body":"The body was empty"},"languageSpecificMessages":[{"property":"message","message":"Der er fejl i de angivne data"},{"property":"Body","message":"The body was empty"}],"errorMessageList":[{"Code":"Body","Message":"The body was empty"}]}
Here is the same code taken from postman. It works fine.
import requests
url = "https://api.dinero.dk/v1/257403/contacts"
payload = "{\r\n \"Name\": \"Test Name\",\r\n \"CountryKey\": \"DK\",\r\n \"IsPerson\": true\r\n}"
print(payload)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ****'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
I hope someone can explain why my code isn't working.
Requests has a json= param you could use:
response = requests.post(url, headers=headers, json=payload)
Docs here.
In your second call, you want to json dump the payload:
import json
response = requests.post(url, headers=headers, data=json.dumps(payload))
Postman has already serialised the payload as a json formatted string. You can do the same with json.dumps().
My aim is to post a comment to a particular post id using Facebook graph API.
This is the code snippet for the same:
url = 'https://graph.facebook.com/v2.11/<post_id>/comments'
parameters = {'access_token': <FACEBOOK_ACCESS_TOKEN>, 'message': 'test comment'}
headers = {"content-type": "application/json"}
parameters = json.dumps(parameters)
response = requests.post(url, data=parameters, headers=headers, timeout=10)
I am calling this API inside my DJANGO POST API.
For Some Reason, Calling the Facebook API through this code doesnt work. The API call gets timeout after 10 seconds.
If I call the Facebook API through Postman / YARC , the comment gets posted successfully.
Can any one tell me where I am going wrong?
Python Requests example:
import requests
url = "https://graph.facebook.com/v2.11/yourPostId/comments"
querystring = {"access_token":"yourtoken"}
payload = "message=test%20comment"
headers = {
'content-type': "application/x-www-form-urlencoded",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
Python http.client example:
import http.client
conn = http.client.HTTPSConnection("graph.facebook.com")
payload = "message=test%20comment"
headers = {
'content-type': "application/x-www-form-urlencoded",
'cache-control': "no-cache"
}
conn.request("POST", "/v2.11/yourPostId/comments?access_token=yourtoken", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
I'm trying to send a binary image file to test the Microsoft Face API. Using POSTMAN works perfectly and I get back a faceId as expected. However, I try to transition that to Python code and it's currently giving me this error:
{"error": {"code": "InvalidImage", "message": "Decoding error, image format unsupported."}}
I read this SO post but it doesn't help. Here's my code for sending requests. I'm trying to mimic what POSTMAN is doing such as labeling it with the header application/octet-stream but it's not working. Any ideas?
url = "https://api.projectoxford.ai/face/v1.0/detect"
headers = {
'ocp-apim-subscription-key': "<key>",
'content-type': "application/octet-stream",
'cache-control': "no-cache",
}
data = open('IMG_0670.jpg', 'rb')
files = {'IMG_0670.jpg': ('IMG_0670.jpg', data, 'application/octet-stream')}
response = requests.post(url, headers=headers, files=files)
print(response.text)
So the API endpoint takes a byte array but also requires the input body param as data, not files. Anyway, this code below works for me.
url = "https://api.projectoxford.ai/face/v1.0/detect"
headers = {
'ocp-apim-subscription-key': "<key>",
'Content-Type': "application/octet-stream",
'cache-control': "no-cache",
}
data = open('IMG_0670.jpg', 'rb').read()
response = requests.post(url, headers=headers, data=data)
print(response.text)