Trying to upload image with Admin API but get 400 http code

Problem in topic. Im using python 3.8, my code below:

filename = f"{url['url'].split('/')[-1][:11]}.jpg"
open(filename, 'wb').write(requests.get(url['url']).content)
filename, content = os.path.basename(open(filename, 'rb').name), open(filename, 'rb')
content_type, _ = mimetypes.guess_type(filename)
file_arg = (filename, content, content_type)

photourl ="https://{admin_domain}/ghost/api/admin/images/upload/",
    headers = {'Content-Type': 'application/json', "Authorization": "Ghost {token}", "Accept-Version": "5.59", "Content-Type" : 'multipart/form-data;'},
    files = {'file': file_arg},
    params = {'purpose': 'image', 'ref': filename}

Are you receiving any other error messages?

How are you generating the token? Are you able to use the API with other methods?

oh i found my error while generating token, but now have another error

{"errors":[{"message":"Please select an image.","context":null,"type":"ValidationError","details":null,"property":null,"help":null,"code":null,"id":"45186620-467c-11ee-ab06-a7253d0c2a45","ghostErrorCode":null}]}

The error indicates that the API isn’t receiving your image.

I’d debug a couple basic things to see where the error might be:

  1. Is the file an image?
  2. Would reusing the file object help?
  3. Content type is specified twice
  4. ensure file_arg is formatted properly
  1. Yes, it image and it opens properly in my editor
  2. No, it dont helps
  3. I fixed and dont help. Headers: {"Authorization": f"Ghost {token}", "Accept-Version": "5.59", "Content-Type" : 'multipart/form-data;'}
  4. file_arg gives ('UljQo5e0V8U.jpg', <_io.BufferedReader name='UljQo5e0V8U.jpg'>, 'image/jpeg') while printing them. It right

I’m not a Python expert, but do you need a read method on the file content?

I copied this part of code from library ghost_client on Github. And most guides on Google says what open(filename, 'rb') right solution

I deleted Content-Type header and it works. Thanks for your help, it helps me

And can you help with creating post? Error - {"errors":[{"message":"An unexpected error occurred, please try again.","context":"Unexpected token p in JSON at position 0","type":"InternalServerError","details":null,"property":null,"help":null,"code":"UNEXPECTED_ERROR","id":"224b8510-46df-11ee-ab06-a7253d0c2a45","ghostErrorCode":null}]}
And my code -"", data={
    "posts": [
            "title": text,
            "lexical": {"root":{"children":[{"type":"gallery","version":1,"images":images,"caption":""},{"children":[],"direction":'null',"format":"","indent":0,"type":"paragraph","version":1}],"direction":'null',"format":"","indent":0,"type":"root","version":1}},
            "feature_image": urls[0],
            "status": "published",
            "authors": [ {"id": "64ecd3b716aaef000168127d"} ],
            "tags": [{ "name": "Из ВКонтакте", "description": "Этот пост был переслан из Вконтакте" }]
        headers = {"Content-Type": "application/json", "Authorization": f"Ghost {token}", "Accept-Version": "5.59"}