Python POST request with form-data

mgrant2012
mgrant2012 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018

Commented:
Why not supply it a json  argument?

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

Open in new window


See also: https://www.w3schools.com/python/ref_requests_post.asp

Author

Commented:
That's what I was trying originally. The product's dev described their api as "messed up" in that the content needs to be passed up as form data, but with a content type of 'application/json' in the header. When passing as json we get a 500 response. Otherwise, the response is 'content-type application/json not supported'.

I've attached 3 screenshots of the parameters/headers/body of the postman request that does work... however when translated to python, it does not. Below you will also see a program I wrote in node.js which also works, however we need this to work in python. I am not well versed enough in node/javascript to complete the project in that language.

Thank you for your time.

node.js of the request which works:
var request = require('request');
var options = {
  'method': 'POST',
  'url': 'https://api.kenect.com/v1/conversations/messages?contactPhone=15857396811',
  'headers': {
    'x-api-token': '1130FxmIcc6uZ***Cpq8ODQo',
    'x-api-key': 'pdwz0naW5hy****6BZFS28',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  formData: {
    'messageBody': 'test sms',
    'locationId': '2045'
  }
};

request(options, function (error, response) { 
  if (error) throw new Error(error);
  console.log(response.body);
});

Open in new window

1.PNG
2.PNG
3.PNG

Author

Commented:
Also- please feel free to test yourself using the api token/key and URL found within the code to see if you're able to successfully post a request, if you like. I should have explained that the purpose of this request is to send an sms message... so a successful request will send the message found in the 'messageBody' parameter to the phone number in the contactPhone parameter. Feel free to leave my phone number in there if you'd like to give it a try.
Thanks again
The api required a content-type of multipart/form-data and for the payload to be formatted as such.
nociSoftware Engineer
Distinguished Expert 2018

Commented:
Better change your credentials with the service and/or at this posting  to remove valid data.
To prevent misuse.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial