pythonpython-3.xrest

PiXhost's API- upload images


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)```

Solution

  • 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.