Avatar of mgrant2012
mgrant2012

asked on 

Python POST request with form-data

We're working with an API that has been giving us trouble from the very beginning. The support people informed us of a bug in which their servers expect a content-type of 'application/json' in the header but for the data to actually be passed using the 'form-data' or application/x-www-urlencoded' content type. No matter what I do I get either a 500 response or an error about the content-type in the response from their server when using the python requests library. I did have this working in javascript/node.js and postman, but we need it to work in python.

Here's the code:
import requests

API_URL = "https://api.kenect.com/v1/conversations/messages/"

headers = {
    'x-api-token': '1130FxmIcc6uZWcq8OXoCZZPCpq8ODQo',
    'x-api-key': 'pdwz0naW5hyCeG9SyJ3PpnOf26BZFS28'
}

data = {
    "locationId": 2045,
    "messageBody": 'test',
    "contactPhone": 15857396811       
}

r=requests.post(url=API_URL, headers=headers, data=data)
print(headers)
print(data)
print(r)

Open in new window

ProgrammingPython

Avatar of undefined
Last Comment
noci

8/22/2022 - Mon