Search code examples
pythonjsonapirequesteloqua

Python bad request error calling Eloqua API - JSON formatting


When I try to access the Eloqua API with the following code, I get a 400 error. Eloqua Support tells me the following:

"It seems that the 400 error is being caused by an unfamiliar format being using in the request's body. We suggest using the JSON form at advised."

Is there something about this call that is not putting the call body in JSON format? How can I do that?

auth = auth_string.encode('base64','strict')

url='https://secure.p03.eloqua.com/api/bulk/2.0/activities/exports'
data= {"name":"Activity Export Test", 
"fields":{
      "ActivityId":"{{Activity.Id}}",
   }, 
"filter":"'{{Activity.Type}}'='Subscribe'"}
data2 = json.dumps(data)
headers = {'Authorization': "Basic %s" % auth}
r =  requests.post(url, data=data2, headers=headers)

Solution

  • You are missing the Content-Type header. Try adding it, like so:

    headers = {
        "Authorization": "Basic %s" % auth,
        "Content-Type": "application/json"
    }