简体   繁体   中英

Can't post file using python requests - Translation from curl

The following command works, but I can't reproduce it using python-requests (2.18.4) :

curl -X POST -H "Authorization: Bearer ..." \
             -H "Content-Type: multipart/form-data" \
             -F 'metadata={...} 
             -F 'data=@data.bz2;type=application/octet-stream' 
              https://www....com

Using the send_devices below, I receive "Unsupported Media Type""

def send_devices(basic_auth):
    endpoint_api = ' https://www....com'
    with open('data.bz2','rb') as payload:
        response = requests.post(endpoint_api, data={'metadata': ...,
                                                     'data': payload},
                                 headers={'Authorization': 'Bearer {0}'.format(basic_auth})

After some comments, I also tried and the error now is "Invalid Metadata Json String":

def send_devices(basic_auth):
    endpoint_api = ' https://www....com'
    files = {'file': ('data.bz2', open('data.bz2', 'rb'), 'application/octet-stream')}
    response = requests.post(endpoint_api, data={"metadata": {"extensions":{"urnType":"IDFA"}}},
                             files=files, headers={'Authorization': 'Bearer {0}'.format(basic_auth)})

Make sure the payload is formatted correctly. It appears you are missing an '{' in your second object within your payload.

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415

You should put it as file (not as data).

r = requests.post(url, files={'file': open('data.bz2', 'rb')})

works fine.

In the first example, was missing the file type

'data': ('data.bz2', open('data.bz2', 'rb'), 'application/octet-stream'),

In the second example, is necessary to add extra post data on the same files dict. Even if is not a dict:

'metadata': ('metadata.csv', json.dumps({"extensions": ...}))}

The solution:

def send_devices(basic_auth):
    endpoint_api = ' https://www....com'
    files = {'data': ('data.bz2', open('data.bz2', 'rb'), 'application/octet-stream'),
            'metadata': ('metadata.csv', json.dumps({"extensions": ...}))}
    response = requests.post(endpoint_api, files=files,
                             headers={'Authorization': 'Bearer {0}'.format(basic_auth)})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM