Link to home
Start Free TrialLog in
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

Avatar of noci
noci

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
Avatar of mgrant2012

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of mgrant2012
mgrant2012

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Better change your credentials with the service and/or at this posting  to remove valid data.
To prevent misuse.