0
            session = requests.Session()
            url = "...api/auth/login"
            payload = {
                "userName": "[email protected]",
                "password": "password"
            }
            headers = {'content-type': 'application/json'}
            response = session.post(url, data=json.dumps(payload), headers=headers)
           
            png_uri = DataURI.from_file(image_path)

            files = {
                'file': (
                    os.path.basename(image_path),
                    png_uri,
                    'image/png'
                ),
                'filename': os.path.basename(image_path),
                'upload': '',
            }
            response = session.post(upload_url, files=files)
            print(response.text)

As shown in the code above I open the image and convert it to dataURI that I send to the endpoint via a POST request.

The issue is that I'm getting an error:

{"error code":400,"message":"Invalid file type"}

Any idea what am I missing here?

2
  • Do you have the API documentation (Swagger\something similar)? If so please share it Commented Nov 20, 2020 at 22:50
  • Unfortunately, the API is private and doesn't contain any "body" examples Commented Nov 20, 2020 at 23:19

2 Answers 2

1

So Im not sure how the api you are posting to handles files.

but I created a very simple post to webhook.site so you can see the json data you are sending

I think what is going wrong with your post is this part. The Api that receives the data might not be able to parse the 'file' data.

files = {
    'file': (
        os.path.basename(image_path),
        png_uri,
        'image/png'
    ),
    'filename': os.path.basename(image_path),
    'upload': '',
}

So instead maybe lay it out like this:

files = {
    'file': png_uri,
    'type': 'image/png',
    'filename': os.path.basename(image_path),
    'upload': ''
}

Here is the the difference in json to an api: Your post: Your Image

My post: My Image

Sign up to request clarification or add additional context in comments.

Comments

0

Apparently, it was much easier than that.

The API was just badly documented...

files = {
                'upload': (
                    os.path.basename(image_path),
                    image_fp,
                    'image/png',
                )
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.