Вопрос

I'm using Python requests module, but whatever I've tried to upload image, it succeeds, but image has errors when opening/reading. I encode the image as base64, set content-type headers (image/png, image/jpeg etc...) etc...

Anyhow, I do the following using CURL and it works:

curl -u test@test.ca:test -H 'Content-Type: image/jpeg' --data-binary @test.jpeg -X POST 'https://test.test.com/api/upload.json?filename=test.jpeg'

What would be the equivalent of this request with the requests module in python (headers etc...)?

Это было полезно?

Решение

To reproduce your curl command, you don't need to encode the image in base64: --data-binary @test.jpeg curl option sends test.jpeg file as is:

import requests

r = requests.post('https://example.com/api/upload.json?filename=test.jpeg', 
                  data=open('test.jpeg', 'rb'), 
                  headers={'Content-Type': 'image/jpeg'},
                  auth=('test@test.ca', 'test')) # username, password

Другие советы

headers = {'Content-Type' : 'image/jpeg'}
params = {'filename' : 'test.jpg'}
r = requests.post("https://test.test.com/api/upload.json",
                   auth=('user','pw'), headers=headers, params=params)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top