Not able to create Order through Python

152 views
Skip to first unread message

ash...@g64.us

unread,
Jul 19, 2016, 2:23:04 PM7/19/16
to PocketMath API
Hi,

I am trying to access your APIs in python. I want to create an order using my code. But I am getting errors. I have a python dictionary with only the required fields for creating order. I am calling your create order API. 

Following are the components of the request

URL - https://api.pocketmath.com/v2/orders.json


input_object ={'campaign_id':135543, 'name':'order2', 'total_budget': 10.0, 'cpm': 0.5, 'schedules':[{'start_at':'2016-07-21T08:04:00.000Z','end_at':'2016-07-25T08:04:00.000Z'}], 'creative_id':300307,'country_code':'US'}

auth  = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json', 'Authorization': 'Token token=559f0b23afc0740b420c9b7d1e3f16288d85347ab4be8de330ffbf7cc2ee24cd'}

I am using the python library requests and calling the following method.

requests.post('https://api.pocketmath.com/v2/orders.json', data = input_object, headers = auth)

If I convert the data object into JSON then I get the following response.

500 Internal Server Error

If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.


If I send it as it is then I get empty response body with error code 422. 

Just to note that I used the data object without converting to JSON for creating campaigns and it worked. Also I created the same order through your API reference page, in the try out section as well as with the curl command and that also worked. I don't understand what is going wrong. Please help me.

Thank you,
Ashwini Prabhu


don...@pocketmath.com

unread,
Jul 19, 2016, 11:44:43 PM7/19/16
to PocketMath API
Hi Ashwini,

We believe your line: 

requests.post('https://api.pocketmath.com/v2/orders.json', data = input_object, headers = auth)

would need to be changed to:

requests.post('https://api.pocketmath.com/v2/orders.json', params = input_object, headers = auth)

Could you please try this and let me know if it helps?
Also one thing wasn't really clear from your email. You mentioned that you managed to create a campaign without converting to JSON, correct? If so, why are you converting to JSON when trying to create an order?

Best regards,

Donald

ash...@g64.us

unread,
Jul 19, 2016, 11:53:24 PM7/19/16
to PocketMath API
Tried the modified code. It gave error 422 with empty response. Same as the previous code.


I tried converting to JSON because the non-JSON data didn't work and I read on some blogs that converting to JSON may resolve errors. So I was just trying with a different way.

ashwini...@gmail.com

unread,
Jul 20, 2016, 2:08:25 AM7/20/16
to PocketMath API
Tried with http.client.HTTPSConnection as well. Still getting error 422 Unprocessable Entity.

Mike H

unread,
Nov 21, 2016, 10:09:01 AM11/21/16
to PocketMath API
Was this resolved?

I'm getting the same error using Python requests. 

Mike H

unread,
Nov 21, 2016, 12:30:26 PM11/21/16
to PocketMath API
Solved.  

Change: 

'Content-Type': 'application/x-www-form-urlencoded'

to: 

'Content-Type': 'application/json'

Eyal

unread,
Mar 9, 2017, 9:37:39 AM3/9/17
to PocketMath API
Hi,
I am have the same issue.

input_object = {'campaign_id': 123456,'name': 'order2333','total_budget': 100.0,'cpm': 0.5,
'schedules': [{'start_at': '2017-07-21T08:04:00.000Z','end_at': '2017-07-25T08:04:00.000Z'}],
'creative_id': 123456,'country_code': 'US'}
headers = {'Content-Type': 'application/json; charset=utf-8','Accept': 'application/json',
'Authorization': 'Token token=XXXXXXXXXXXXXXXXXXXXXXXX'}
r = requests.post('https://api.pocketmath.com/v2/orders.json',params=input_object,headers=headers)

Keep getting 422.

Thanks


Mike H

unread,
Mar 9, 2017, 9:53:28 AM3/9/17
to PocketMath API
Try changing params=input_object to data=input_object 

Eyal

unread,
Mar 9, 2017, 10:03:39 AM3/9/17
to PocketMath API
Thanks. Now I get 500.
When I change the content-type: to application/x-www-form-urlencoded, i get 422 again.

If i remove the schedules parameter, I get a description in 422 that "schedules" parameter is missing.
So it must be some other parameter that is wrong or missing.

Mike H

unread,
Mar 9, 2017, 10:18:34 AM3/9/17
to PocketMath API
Try input_data = json.dumps(input_data) before the post request. 

Eyal

unread,
Mar 9, 2017, 10:23:48 AM3/9/17
to PocketMath API
Thanks!
This helped.

I ended up with this code:

input_object = {'campaign_id': 1111111,'name': 'order2333','total_budget': 100.0,'cpm': 1,

'schedules': [{'start_at': '2017-07-21T08:04:00.000Z','end_at': '2017-07-25T08:04:00.000Z'}],
                'creative_id': 1111111,'country_code': 'US', 'exchanges':['OpenX'] }
headers = {'Content-Type': 'application/json',
'Authorization': 'Token token=XXXXXXXXXXXXXXXXXXXXXx'}

input_data = json.dumps(input_object)
r = requests.post('https://api.pocketmath.com/v2/orders.json',data=input_data,headers=headers)




Reply all
Reply to author
Forward
0 new messages