import json
import requests
user_f = open('categories.json',)
users = json.load(user_f)
media_api = 'http://127.0.0.1:8000/api/categories/'
files=[
('category_image', ("Traditional-Art.png", open("Traditional-Art.png",'rb'),'image/jpeg'))
]
json = {
"category_name": "cat",
"category_description": "desc",
"user": 16
}
headers = {
"Accept": 'application/json, text/plain, */*',
"enctype":"multipart/form-data"
}
response = requests.request("POST", media_api, headers=headers, data=json, files=files)
print(response.json())
Here, i am trying to upload image to django rest-framework and getting below error
`{u'category_image': [u'Upload a valid image. The file you uploaded was either not an image or a corrupted image.']}`
this works when image is jpg format but, not working in png .
Am i missing anything or do i need to modify something ?
Please take a look
what you have done is wrong either you try to send the data as json or as form do something like it may help you.
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
multipart_data = MultipartEncoder(
fields={
# a file upload field
'file': ('file.zip', open('file.zip', 'rb'), 'text/plain')
# plain text fields
'field0': 'value0',
'field1': 'value1',
}
)
response = requests.post('http://httpbin.org/post',
data=multipart_data,
headers={'Content-Type': multipart_data.content_type})
Related
I am trying to query Adobe PDF services API to generate (export) DOCX from PDF documents.
I just wrote a python code to generate a Bearer Token in order to be identified from Adobe PDF services (see the question here: https://stackoverflow.com/questions/68351955/tunning-a-post-request-to-reach-adobe-pdf-services-using-python-and-a-rest-api). Then I wrote the following piece of code, where I tried to follow the instruction in this page concerning the EXPORT option of Adobe PDF services (here: https://documentcloud.adobe.com/document-services/index.html#post-exportPDF).
Here is the piece of code :
import requests
import json
from requests.structures import CaseInsensitiveDict
N/B: I didn't write the part of the code generating the Token and enabling identification by the server
>> This part is a POST request to upload my PDF file via form parameters
URL = "https://cpf-ue1.adobe.io/ops/:create?respondWith=%257B%2522reltype%2522%253A%2520%2522http%253A%252F%252Fns.adobe.com%252Frel%252Fprimary%2522%257D"
headers = CaseInsensitiveDict()
headers["x-api-key"] = "client_id"
headers["Authorization"] = "Bearer MYREALLYLONGTOKENIGOT"
headers["Content-Type"] = "application/json"
myfile = {"file":open("absolute_path_to_the_pdf_file/input.pdf", "rb")}
j="""
{
"cpf:engine": {
"repo:assetId": "urn:aaid:cpf:Service-26c7fda2890b44ad9a82714682e35888"
},
"cpf:inputs": {
"params": {
"cpf:inline": {
"targetFormat": "docx"
}
},
"documentIn": {
"dc:format": "application/pdf",
"cpf:location": "C:/Users/a-bensghir/Downloads/P_D_F/trs_pdf_file_copy.pdf"
}
},
"cpf:outputs": {
"documentOut": {
"dc:format": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"cpf:location": "C:/Users/a-bensghir/Downloads/P_D_F/output.docx"
}
}
}"""
resp = requests.post(url=URL, headers=headers, json=json.dumps(j), files=myfile)
print(resp.text)
print(resp.status_code)
The status of the code is 400
I am tho well authentified by the server
But I get the following as a result of print(resp.text) :
{"requestId":"the_request_id","type":"Bad Request","title":"Not a multipart request. Aborting.","status":400,"report":"{\"error_code\":\"INVALID_MULTIPART_REQUEST\"}"}
I think that I have problems understanding the "form parameters" from the Adobe Guide concerning POST method for the EXPORT job of the API (https://documentcloud.adobe.com/document-services/index.html).
Would you have any ideas for improvement. thank you !
Make you variable j as a python dict first then create a JSON string from it.
What's also not super clear from Adobe's documentation is the value for documentIn.cpf:location needs to be the same as the key used for you file. I've corrected this to InputFile0 in your script. Also guessing you want to save your file so I've added that too.
import requests
import json
import time
URL = "https://cpf-ue1.adobe.io/ops/:create?respondWith=%257B%2522reltype%2522%253A%2520%2522http%253A%252F%252Fns.adobe.com%252Frel%252Fprimary%2522%257D"
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/json, text/plain, */*',
'x-api-key': client_id,
'Prefer': "respond-async,wait=0",
}
myfile = {"InputFile0":open("absolute_path_to_the_pdf_file/input.pdf", "rb")}
j={
"cpf:engine": {
"repo:assetId": "urn:aaid:cpf:Service-26c7fda2890b44ad9a82714682e35888"
},
"cpf:inputs": {
"params": {
"cpf:inline": {
"targetFormat": "docx"
}
},
"documentIn": {
"dc:format": "application/pdf",
"cpf:location": "InputFile0"
}
},
"cpf:outputs": {
"documentOut": {
"dc:format": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"cpf:location": "C:/Users/a-bensghir/Downloads/P_D_F/output.docx"
}
}
}
body = {"contentAnalyzerRequests": json.dumps(j)}
resp = requests.post(url=URL, headers=headers, data=body, files=myfile)
print(resp.text)
print(resp.status_code)
poll = True
while poll:
new_request = requests.get(resp.headers['location'], headers=headers)
if new_request.status_code == 200:
open('test.docx', 'wb').write(new_request.content)
poll = False
else:
time.sleep(5)
I don't know why the docx file (its well created by the way) doesn't open, telling via popup that the content is not readable. maybe it's due to the 'wb' parsing methos
I had the same issue. Typecasting to 'bytes' the request contents solved it.
poll = True
while poll:
new_request = requests.get(resp.headers['location'], headers=headers)
if new_request.status_code == 200:
with open('test.docx', 'wb') as f:
f.write(bytes(new_request.content))
poll = False
else:
time.sleep(5)
This is my first project using API/python. Basically, I want to get information from trackhive.com to see where is the package. But first, to understand how it works, I'm just trying to create a track.
On the website, they give me this example:
from urllib2 import Request, urlopen
values = """
{
"tracking_number": "9361289676090919095393",
"slug": "usps"
}
"""
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer Token'
}
request = Request('https://private-anon-6175dd5596-trackhive.apiary-proxy.com/trackings
', data=values, headers=headers)
response_body = urlopen(request).read()
As I'm using Python 3, my code is
import json
import urllib.request
values = """
{
"tracking_number": "9361289676090919095393",
"slug": "usps"
}
"""
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer mytokenAPI'
}
url = 'https://private-anon-6175dd5596-trackhive.apiary-proxy.com/trackings'
request = urllib.request.Request(url,data=values, headers=headers)
response_body = urllib.request.urlopen(request, data=bytes(json.dumps(headers), encoding="utf-8")).read()
But when it calls the "urllib.request.urlopen", it returns "HTTPError: Bad Request". What am I doing wrong?
On the website, they said that the endpoint is located at: https://api.trackinghive.com. In their code, they are accessing: 'https://private-anon-6175dd5596-trackhive.apiary-proxy.com/trackings'. If I haven't access to their example, how could I know that I suppose to access this URL, and not something like "https://api.trackinghive.com/trackings"? Isn't it suppose to use the endpoint somewhere?
(I'm sorry about all these questions, I'm a little confused about how these codes with API and URL works)
I appreciate any help ^^
Edit:
Link to documentation:
https://trackhive.docs.apiary.io/#introduction/api-requirements/end-point
If I register on https://my.trackinghive.com/login and generate API key then code works for me.
I use requests instead of urllib because it is easier to create POST request. And documentation shows that it has to be POST, not GET.
And it is easier to send JSON data. It doesn't need header Content-Type': 'application/json' because requests will add it automatically when is use json=.... And it doesn't need to convert values to one string because requests will do it automatically.
BTW: I don't need module json to work with data. I use it only to convert it to string with indentation to display it.
import requests
values = {
"tracking_number": "9361289676090919095393",
"slug": "usps"
}
token = 'eyJh....'
headers = {
'Authorization': f'Bearer {token}' # both works for me
#'Authorization': f'{token}'
}
url = 'https://api.trackinghive.com/trackings'
r = requests.post(url, headers=headers, json=values)
print('\n--- as text ---\n')
print(r.text)
print()
print('\n--- as dict/list ---\n')
data = r.json()
print('keys:', list(data.keys()))
print('message:', data["meta"]["message"][0])
# - show it more readable -
print('\n--- more readable ---\n')
import json
text = json.dumps(data, indent=2)
print(text)
Result:
--- as text ---
{"meta":{"code":409,"message":["Tracking already exists."]},"data":{"_id":"60a5b5a0aa4e400011a0c657","current_status":"Pending","return_to_sender":false}}
--- as dict/list ---
keys: ['meta', 'data']
messge: Tracking already exists.
--- more readable ---
{
"meta": {
"code": 409,
"message": [
"Tracking already exists."
]
},
"data": {
"_id": "60a5b5a0aa4e400011a0c657",
"current_status": "Pending",
"return_to_sender": false
}
}
I'm trying to test the Shazam API Detect feature on a mono sample as provided by this tutorial.
My code reads the raw audio file and sends it as base64 plaintext in the body of a POST requests to the Shazam API.
The audio file can be downloaded via this link:
import requests
import base64
def shazam(payload):
url = "https://shazam.p.rapidapi.com/songs/detect"
payload = open(payload,"rb").read()
payload = base64.b64encode(payload)
payload = str(payload)
headers = {
'x-rapidapi-host': "shazam.p.rapidapi.com",
'x-rapidapi-key':str(open("./api.txt","r").read().strip()),
'content-type': "text/plain",
'accept': "text/plain"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
shazam("/home/samples/mono.raw")
Any ideas where I'm going wrong?
I'am having problems with this API as well been searching up and down to find a solution :(
it work on me
from pydub import AudioSegment
import base64
import requests
import json
file_path="./test.raw"
url = "https://rapidapi.p.rapidapi.com/songs/detect"
encode_string = base64.b64encode(open(file_path, "rb").read())
payload=encode_string
print(type(payload))
headers = {
'content-type': "text/plain",
'x-rapidapi-key': "<<<you key>>>",
'x-rapidapi-host': "shazam.p.rapidapi.com"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(json.dumps(json.loads(response.text)))
I have some json containing some thai values. It looks something like
{
"TitleName": "คุณ",
"FirstName": "Simar"
}
I need to make a Http POST request with this json body with the exact thai value.I am using Python 3 requests library to make the request.
I tried this
headers = {
'Content-Type': "application/json",
'Authorization': "xxx",
'cache-control': "no-cache",
'Postman-Token': "xxx"
}
response = requests.request("POST", url, json=request, headers=headers)
It generates json values as
"TitleName": "\\u0e04\\u0e38\\u0e13",
"FirstName": "Simar"
I also tried this
json_request = json.dumps(self.request_data,ensure_ascii=False).encode('utf8')
response = requests.request("POST", url, json=json_request, headers=headers)
It generates json values as
"TitleName": "\xe0\xb8\x84\xe0\xb8\xb8\xe0\xb8\x93",
"FirstName": "Simar"
But I want json values to be generated as
"TitleName": "คุณ",
"FirstName": "Simar"
Help will be appreciated. Thanks in advance.
To preserve non-ascii characters in POST requests you need to serialise to json manually, and explicitly set the content-type header.
data = json.dumps(my_dict, ensure_ascii=False)
r = requests.post(url, headers={'content-type': 'application/json'},
data=data.encode('utf-8'))
So I built an API which takes in a pdf file and json and converts the file into text. Testing with Postman works fine, however now I try to make a script to send multiple images and the API does not receive the image I send it in the script. It receives the request but not its contents. Additionally I don't get the json file either, while it does show on the script side.
I looked at the Postman request and implemented it in the script, however it still does not work. I tried sending only the file without a json and could not get it working. I've been looking in the documentation of flask and request, but I'm not able to find the explanation why it does not receive the image.
#Script code
import requests
import time
import glob
url = "http://127.0.0.1:5000/transcribe"
for file in glob.glob("/Receipts_to_scan/*.pdf"):
print(open(file, "rb"))
files = {
'file': open(file, 'rb'),
'json': '{"method":"sypht"}'
}
headers = {
'Accept': "application/pdf",
'content-type': "multipart/form-data",
'Connection': 'keep-alive'
}
response_decoded_json = requests.post(url, files=files, headers=headers)
time.sleep(5)
print(response_decoded_json)
#--------------------------
#API code
from flask import Flask, request
#app.route("/transcribe", methods = ["POST"])
def post():
#Getting the JSON data with all the settings in it
json_data = request.files["json"]
print(json_data)
image = request.files["file"]
print(image)
Could you try the following? This way you can combine a file and other data (like a dictionary) in a request.
Change your Flask API:
#Getting the JSON data with all the settings in it
json_data = request.form # <--- change this line
print(json_data)
And then make a request like this (without manually setting the headers):
files = {
'file': (file, open(file, 'rb'), "application/pdf")
}
data = {
"method": "sypht"
}
response_decoded_json = requests.post(url, files=files, data=data)
time.sleep(5)
print(response_decoded_json)
This should give you an ImmutableMultiDict and a FileStorage object to work with.
Your API then prints:
ImmutableMultiDict([('method', 'sypht')])
<FileStorage: 'test.pdf' ('application/pdf')>