Trying to upload a file using requests module, but encountered Internal Server Error Its the same using poster module too:
import requests
url = "abc.com/upload"
querystring = {"ft":"1","fn":"filename"}
payload = ""
files={'file': open(r'Users/.../test.zip', 'rb')}
headers_info = {
'content-type': "multipart/form-data; boundary=---12345",
'x-api-service-version': "1.0",
'connection': "Keep-Alive",
'authorization': "Basic XXXXXXX",
'x-file-format': "decrypted",
'cache-control': "no-cache",
}
response = requests.post(url, data = payload , headers=headers_info , params=querystring , files=files)
print response.status_code
print response.text
I tested the api with POSTMAN (chrome extension to test rest API) and it seems to work fine with postman i get a success response and the file is uploaded.
The postman code for python shows :
import requests
url = "abc.com/upload"
querystring = {"ft":"1","fn":"filename"}
payload = ""
headers = {
'content-type': "multipart/form-data; boundary=---12345",
'accept-encoding': "gzip, deflate",
'x-api-service-version': "1.0",
'connection': "Keep-Alive",
'authorization': "Basic XXXXXXX",
'x-file-format': "decrypted",
'cache-control': "no-cache",
'postman-token': "XXXXXXX"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
Any suggestions for the same ? Am I missing something obvious? Thanks for any pointers you can share!
You don't have to specify 'content-type': "multipart/form-data; boundary=---12345", as well as empty data. Try to send request without headers
response = requests.post(url, params=querystring , files=files)
If you fail you might try to add 'authorization': "Basic XXXXXXX", 'postman-token': "XXXXXXX" headers
Related
I'm trying to POST to a discord webhook URL using Python Requests but whenever the embeds field is present, it returns {'code': 50109, 'message': 'The request body contains invalid JSON.'}. If I remove embeds and just leave content it will send without any errors.
My code is:
url = "https://discord.com/api/webhooks/[redacted]/[redacted]"
headers = {
"Content-Type": "application/json"
}
data = {
"username": "Webhook",
"content": "Hello, World!",
"embeds": [{
"title": "Hello, Embed!",
"description": "This is an embedded message."
}]
}
res = requests.post(url, headers=headers, data=data)
I've tried various version of the Discord API but the result is always the same.
I got it working by replacing
requests.post(url, headers=headers, data=data)
with
requests.post(url, json=data)
Try this. I think the requests library may be adding a header called content-type which conflicts with your header Content-Type, which makes the Discord API return an error:
url = "https://discord.com/api/webhooks/[redacted]/[redacted]"
headers = {
"content-type": "application/json"
}
data = {
"username": "Webhook",
"content": "Hello, World!",
"embeds": [{
"title": "Hello, Embed!",
"description": "This is an embedded message."
}]
}
res = requests.post(url, headers=headers, json=data)
I need to upload media wordpress api in flutter app, now I can upload media from wp api but the problem is the image after uploaded is broken and image size is very small, please help me
var resposeimg = await http.post(
'example.com/wp-json/wp/v2/media',
body: jsonEncode({
"file" : file.path.split("/").last,
}),
headers: {
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
"content-type" : "image/${imgformat.replaceAll(new RegExp(r'jpg'), 'jpeg')}",
'Content-Disposition' : 'attachment; filename=${file.path.split("/").last}',
"Authorization": "Bearer ${widget.userToken}"
}
)
import requests
token = {
"access_token": "q3eFsG5ONt6fvdJsB3AAXL38KBAwrdCJ0",
"api_server": "https:\/\/api07.iq.questrade.com\/",
"expires_in": 1800,
"refresh_token": "Y3p43laee75kfDxzDWrONoNpkhgAFUyb0",
"token_type": "Bearer"
}
uri = "https://api01.iq.questrade.com/v1/markets"
headers = {'Authorization': 'Bearer {}'.format(token.get('access_token'))}
rt = requests.get(uri, headers=headers)
response = rt.json()
I do not understand what is happening here because token is updated, but I got <Response [401]>. What do I need to do to fix the problem?
Your mistake is here:
"api_server": "https:\/\/api07.iq.questrade.com\/"
You have \/, you should use the usual one:
"api_server": "https://api07.iq.questrade.com/"
I don't manage to read response headers using browser_client.dart :
import 'package:http/browser_client.dart';
var response =
await client.post(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}, body: body);
print('Response headers: ${response.headers}');
Thanks for your help.
The server needs to allow the browser to expose the headers by listing the headers in the Access-control-expose-headers response header, otherwise you can see them in the browser devtools but when you try to read them in code, the browser will suppress them.
See also
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
Why is Access-Control-Expose-Headers needed?
This Meteor server code uses atmosphere HTTP package. I receive human un readable characters from response.content even though characters are readable fine in the browser.
Why and how to fix that? Thanks
const response = HTTP.call(method, url, {
timeout: 30000,
headers: header,
params: Params,
followRedirects: true
}
);
console.log(response.content);
response header has:
'content-type': 'text/html'
'content-encoding': 'gzip'
request header has:
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/x-www-form-urlencoded"