Reputation: 4656
I am trying to post a request structure over rest api.
My code:
def pack_orders(self, orderItemId, invoiceDate, invoiceNumber, tax, serialNumbers=None, subOrderItemId = None, subSerialNumbers = None, subInvoiceDate = None, subTax = None):
url = "https://api.flipkart.net/sellers/orders/labels"
payload = {[{"orderItemId": orderItemId,
"serialNumbers": serialNumbers,
"invoiceDate": invoiceDate,
"invoiceNumber": invoiceNumber,
"tax": tax,
"subItems": [{
"orderItemId": subOrderItemId,
"serialNumbers": subSerialNumbers,
"invoiceDate": subInvoiceDate,
"tax": subTax}]
}],}
return self.session.post(url, params=payload)
calling the above function:
label = fk.pack_orders(orderItemId='232519872', invoiceDate='2015-08-13', invoiceNumber='INVSTR01', tax=5)
print label.status_code
print label.url
print label.content
It throws error 422. I know it has to do something with the requests parameter structure. I am unable to pinpoint the source of error.
Here is a link to documentation for any help. Documentation
Upvotes: 3
Views: 13916
Reputation: 8960
API is expecting data in JSON
format.
Also python requests is simple and easy to use.
import requests
data = {[{"orderItemId": orderItemId,
"serialNumbers": serialNumbers,
"invoiceDate": invoiceDate,
"invoiceNumber": invoiceNumber,
"tax": tax,
"subItems": [{
"orderItemId": subOrderItemId,
"serialNumbers": subSerialNumbers,
"invoiceDate": subInvoiceDate,
"tax": subTax}]
}],}
r = requests.post(url, json=data)
r.status_code
r.json()
Sample requests (using shell):
Successfully made the request. Got 401 for invalid authentication (for obvious reason)
In [19]: import requests
In [20]: url = "https://api.flipkart.net/sellers/orders/labels"
In [21]: data = [{
....: "orderItemId": 1179576,
....: "serialNumbers": ["IMEI1-UNIT1"],
....: "invoiceDate": "2014-08-29",
....: "invoiceNumber": "INV-01",
....: "tax": 100.98,
....: "subItems": [{
....: "orderItemId": 1173467,
....: "serialNumbers": [ ],
....: "invoiceDate": "2014-08-29",
....: "tax": 10.98
....: }]
....: }]
In [22]: r = requests.post(url, json=data)
In [23]: r.status_code
Out[23]: 401
In [24]: r.json()
Out[24]:
{u'error': u'unauthorized',
u'error_description': u'An Authentication object was not found in the SecurityContext'}
Upvotes: 7
Reputation: 694
You should try:
return self.session.post(url, data=payload)
instead of
return self.session.post(url, params=payload)
Upvotes: 0