HTTP 400 error using Python Requests POST and Intercom API

445 Views Asked by At

I am trying to pull a list of conversations from Intercom using their API, restricted based on the date that they were updated.

Their site (https://developers.intercom.com/intercom-api-reference/reference#search-for-conversations) says

To search for conversations, you need to send a POST request to https://api.intercom.io/conversations/search. This will accept a query object in the body which will define your filters in order to search for conversations.

So I tried:

import requests    
url = 'https://api.intercom.io/conversations/search'
data_params = {
 "query":  {
    "field": "updated_at",
    "operator": ">",
    "value": 1560436784
  }
}
headers = {'Authorization' : 'Bearer ******************', 'Accept':'application/json', 'Content-Type':'application/json'}
r = requests.post(url, headers = headers, data = data_params)

r.status_code

This consistently runs into a 400 'Bad Request' error, but I'm struggling to see why. I've also tried json = data_params in the post call but same result. I appreciate the code might not be fully reproducible as it requires an authenticated API connection, but if there's anything obviously wrong it would be good to know!

2

There are 2 best solutions below

0
On

Obviously, there seems no obviously error here.

for the 400 Bad Request . I think you should do the check of you data_params if there's something missing or in bad format. also the headers

0
On

You should try converting the data_params dictionary to json.

I tested with json.dumps and this seems to have worked:

import json

url = 'https://api.intercom.io/conversations/search'
data_params = {
 "query":  {
    "field": "updated_at",
    "operator": ">",
    "value": 1560436784
  }
}
headers = {'Authorization' : 'Bearer ******************', 'Accept':'application/json', 'Content-Type':'application/json'}

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

print(r.status_code)