0
votes

I'm trying to upload an image to Sirv through python requests. Here is what I have so far;

def upload_files(access_token, filename):
    endpoint = "https://api.sirv.com/v2/files/upload"
    headers = {'Content-Type' : 'application/json', 'authorization': 'bearer {}'.format(access_token)}
    send_request = requests.post(endpoint, headers = headers, data = filename)
    return send_request

access_token = "MY_ACCESS_TOKEN"
filename = {'filename': ('oldman.jpg', open('oldman.jpg', 'rb'))}
upload_files(access_token, filename)

I understand http status codes. However, I fail to understand what I'm doing wrong to get a 400 thrown by the server. Here's the full response I'm getting;

{'_content': b'{\n "statusCode": 400,\n "error": "Bad Request",\n "message": "child 'filename' fails because ['filename' is required]"\n}', '_content_consumed': True, '_next': None, 'status_code': 400, 'headers': {'Date': 'Fri, 26 Jun 2020 13:41:02 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '121', 'Connection': 'keep-alive', 'access-control-allow-origin': '', 'access-control-expose-headers': '', 'cache-control': 'no-cache', 'Server': 'Sirv.API'}, 'raw': <urllib3.response.HTTPResponse object at 0x7f51f39c80a0>, 'url': 'https://api.sirv.com/v2/files/upload', 'encoding': 'utf-8', 'history': [], 'reason': 'Bad Request', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(seconds=7, microseconds=408883), 'request': <PreparedRequest [POST]>, 'connection': <requests.adapters.HTTPAdapter object at 0x7f51f44422b0>}

From the error, I suspect that the server is somehow not detecting my filename parameter. I am using this doc as a guide to querying the API.

Summary: I need help understanding why I'm getting the error.

2

2 Answers

0
votes

try this:

def upload_files(access_token, filename):
    endpoint = "https://api.sirv.com/v2/files/upload"
    headers = {
        "authorization": "bearer {}".format(access_token),
    }
    send_request = requests.post(endpoint, headers=headers, files=filename)
    return send_request

try removing content-type from headers, it will be automatically set by the requests library.

0
votes

I got some help from the Sirv team. Turns out the Sirv REST API, requires not only a path to the local file, but also the path to the uploaded file. In my case, I was declaring only the path to the local file. As a result the server threw status code 400, along with this message;

child 'filename' fails because ['filename' is required]"

Here's the modified code that meets all requirements;

def upload_files(access_token, local_file, upload_path):
    endpoint = "https://api.sirv.com/v2/files/upload"
    headers = {'Content-Type' : 'image/jpeg', 'authorization': 'bearer {}'.format(access_token)}
    upload_path = {'filename': upload_path}
    sirv_api_request = requests.post(endpoint, headers = headers, data = local_file, params = upload_path)

    return sirv_api_request

local_file = open('oldman.jpg', 'rb')
upload_path = '/myfolder/oldman.jpg'
upload_file(access_token, local_file, upload_path)