Search code examples
pythonpandaspython-requestsapi-design

API POST Response only reading last record of dataframe converted to list, how to change?


I am trying to pass data through a POST command into a API. The API only takes data formatted in the Content-Type: application/x-www-form-urlencodedformat. I am not familiar with this format yet. Information about the particular API call I am trying to make can be found here. https://www.activecampaign.com/api/example.php?call=contact_add

The data sits in a dataframe with the following fields.

     email                 p[1]*
0     [email protected]      1
1  [email protected]      1
2   [email protected]      1

According to the documentation p[1] is the field to specify what list I want the data to go into.

The code I am running is as follows:

df1 = pd.DataFrame({'email':['[email protected]','[email protected]','[email protected]'], 'p[1]*':1})

#I think Content-Type: application/x-www-form-urlencoded formated data takes list format#

df1 = df1.to_dict(orient = 'list')

url = 'https://URL/admin/api.php?api_action=contact_add&api_output=json&api_key=123ABC' 

resp = requests.post(url, data=df1, headers=headers)

print(resp.text)

The response object resp returns 200, but in the email campaign list I only see the last email address in row 2 appear only. Why is this ? what am I doing wrong?

Thank you in advance.


Solution

  • Well basically servers confirms creation of objects by sending 201 status code, but some servers return 200 instead. I wonder if you already created these objects... I'm also not sure that you need the asterisk after p[1] (after quick look at the documentation I think that p should only points to the list ID), but that's for you to check.