简体   繁体   中英

Python requests PUT 400 error for missing header

I have been working on using requests to upload data via API. I've done successful GET requests before but this is my first time doing a PUT. It's been a bit of a battle and progress has been slow and steady but I've really hit a wall this error.

{"error":{"code":400,"message":"Bad Request. Missing header (Content-Type: application/json)"}}

It seems to clearly say I am missing the content-type in my header, but as you can see below I do have this included in the header. Any insight would be greatly appreciated.

def api_put():
    url = 'xxxxx'
    headers = { 'Host': 'xxxxx',
                'X-Api-Key': 'xxxxx',
                'Content-Type': 'application/json'}
    payload = { 'docType': 'jts',
                'version': '1.0',
                'header' : {
                    'columns': {
                        '0': {
                            'series': 'Longitude',
                            'name': 'Longitude',
                            'dataType': 'NUMBER'},
                        '1': {
                            'series': 'Latitude',
                            'name': 'Latitude',
                            'dataType': 'NUMBER'}},
                'data': [{
                    'ts': '2021-07-26T15:36:02Z',
                    'f': { '0': {'v': 47.667196253284175},
                         '1': {'v': -122.38602397890254}}
                        }]}}
    response = requests.put(url, headers=headers, data=json.dumps(payload))
    print(response.text)

It's likely the backend problem. Have you ever tried to request https://httpbin.org/anything to ensure you were sending content-type? If so, it's the proxy/backend's problem.
Since the error msg looks like the backend was checking the type by their own code instead of using the server feature. You can try to set the header to lower case.

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