PiXhost's API- upload images

Viewed 506

I'm trying to use PiXhost's api to upload images. Every time I request something from https://api.pixhost.to/images it always returns 400 Bad request. Their API documentation uses python unirest package, but I can't install it as it seems to use too old code.

Am I doing something wrong here?

Link to documentation

my code:

import requests
headers = {
    "Content-Type": "multipart/form-data; charset=utf-8",
    "Accept": "application/json"
}
params = {
    "content_type": "0",
    "max_th_size": "420"
}
files = {
    "img": open("image.jpg", mode="rb")
}

response = requests.post("https://api.pixhost.to/images", headers=headers, params=params, files=files)```
2 Answers

Just a quick test, I managed to get it done via cURL

curl -X POST --include "https://api.pixhost.to/images" \
  -H 'Content-Type: multipart/form-data; charset=utf-8' \
  -H 'Accept: application/json' \
  -F 'img=@test1.jpg' \
  -F 'content_type=0' \
  -F 'max_th_size=420'

https://pixhost.to/show/39/178828640_test1.jpg

Note: the test1 image is in the same folder when I running the script

I also receive 400 status code without a body when removing the @ before the URL (so check you URL again)

HTTP/1.1 400 Bad Request
Server: nginx/1.10.3 (Ubuntu)
import requests

url = "https://api.pixhost.to/images"

payload={'content_type': '0',
'max_th_size': '420'}
files={
    'img': ('1.JPG', open('C:/Users/prave/Desktop/1.JPG', 'rb')),
    'content_type': '0',
    'max_th_size': '420'
}
headers = {

}

response = requests.request("POST", url, data=payload, files=files)

print(response)

Here you go

output :

Response 200

Issue with your code:

THe endpoint expects formdata , you are passing query parameter

So how did i figure it out

enter image description here

goto documentation and click curl tab to get curl equalent code:

Now goto postman and click import>Raw text > paste the curl , and click continue and then import

This creates the request for you

enter image description here

Now goto body and select img type as file and upload the file:

enter image description here

Now click code and generate equalent curl and python request code:

enter image description here

enter image description here

THe generated python code had few isseus one was to remove header completely and then to remove one slash from the file name.

Related