Reputation: 644
A product was purchased to enable our users to send/receive SMS over HTTP. Now it's my job to build it into our current CMS platform & database. It's got a swagger API.
Here is the documentation for the specific POST request I am trying to send: POST: Send an SMS Message
Here is my simple python program to test the functionality. I get a generic 500 internal server error response. What am I doing incorrectly?
import requests
API_URL = "https://api.kenect.com/v1/conversations/messages"
headers = {
'x-api-token': '****************',
'x-api-key': '*******************',
'Content-Type': 'application/json',
}
params = {
'contactPhone': '158572968**',
'locationId': '2045',
'messageBody': 'test sms',
'outgoing': 'true',
}
r=requests.post(url = API_URL, headers = headers, params = params)
print(r)
Upvotes: 1
Views: 14259
Reputation: 5410
There seems to be 2 issues:
Content type and payload encoding.
You are using params
parameter in the post
method. params
is used with get
method for passing data in the URL's query string.
In the post
method, depending on the required content type, you need to use either data
parameter to send form-encoded data:
r=requests.post(url = API_URL, headers = headers, data = params)
or json
parameter to send application/json
payload:
r=requests.post(url = API_URL, headers = headers, json = params)
Remove the 'Content-Type' key from your headers
dictionary. data
and json
parameters will set up correct content type automatically.
outgoing
is not a valid request parameter for the /v1/conversations/messages
resource. This field is from the response object, not the request one. Remove it from your payload.
So to sum up, for form-encoded payload the code should look like this:
import requests
API_URL = "https://api.kenect.com/v1/conversations/messages"
headers = {
'x-api-token': '****************',
'x-api-key': '*******************',
}
params = {
'contactPhone': '158572968**',
'locationId': '2045',
'messageBody': 'test sms',
}
r=requests.post(url = API_URL, headers = headers, data = params)
Upvotes: 2