Reputation: 219
I am trying to POST an image with the requests module from my local client to a service (tillhub.com). Since the api documentation for posting images is missing (at least for me) some important aspects, I am trying to use the Google Chrome Dev Tools to figure out the neccessary parameters. When I upload an image directly from the dashboard, I can see that the following parameters for the header are used:
In addition that, I can see that the following form data is going to be passed:
I am trying to mimic the request from my local client with the following snippet:
import requests
headers =
{'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryOVDXaA33W3zKicwR',
'cache-control': 'no-cache',
'Authorization': 'Bearer .......',
'accept': 'application/json, text/plain, */*'}
files= {'image': ("demo.jpg",open("demo.jpg", "rb"),'image/jpeg')}
requests.post(url=url,files=files, headers=headers).json()
which results in
{'status': 400,
'msg': 'Unexpected token - in JSON at position 0',
'request': {'host': 'api.tillhub.com',
'id': '946f956c-efcf-48e8-aec8-44e16b118a4e'}}
Based on their JavaScript SDK I can see that they just use for the content-type 'image/jpeg':
async create (query: ImagesQuery, payload: FormData): Promise<ImagesResponse> {
const uri = this.uriHelper.generateBaseUri(`/${query.subsystem}/${query.prefix}`)
try {
const response = await this.http
.getClient()
.post(uri, payload, { headers: { 'Content-Type': 'image/jpeg' } })
Am I doing something fundamentally wrong? Would appreciate any kind of help.
Upvotes: 0
Views: 573
Reputation: 361
I was playing with your code block. I have removed the content-type
after reading this question answer.
import requests
headers ={'Authorization': 'Bearer ey......eU','cache-control': 'no-cache'}
files=dict(image=('demo.jpg', open("demo.jpg", "rb"),'image/jpeg'))
requests.post(url=url,files=files, headers=headers).json()
And it worked for me. Can you please try this?
Upvotes: 2