I've been getting an error response from the API (see https://github.com/moltin/issues/issues/22 for more info). After further testing I've found that the issue is being caused by the way the Moltin Python SDK is handling sending JSON payloads to the API.
In moltin.request.Request#post() the SDK is performing the call to the API using the Python Requests package by doing the following:
The function it's calling is requests.api.post(url, data=None, json=None, **kwargs), and the issue is that the Moltin Python SDK is always passing the data parameter and never passing the json parameter. This means that when the Requests library gets to requests.models.PreparedRequest#prepare_body() it attempts to squash a payload that needs to be represented by JSON into POST parameters, which fails for nested JSON.
For example, let's say I'm trying to perform the following with the Moltin Pyton SDK:
Due to moltin.request.Request#post() passing this payload to requests.api.post() as the data parameter instead of the json parameter, the requests.models.PreparedRequest#prepare_body() function will turn this payload into the following:
Then the request to the API obviously fails since the bill_to provided to the API is completely invalid, and it returns a response of 'billing_address': ["Address 'postcode' not found"].
If the payload had instead been passed as the json parameter, the requests.models.PreparedRequest#prepare_body() function would have turned this payload into a JSON string and set the Content-Type header to application/json, and the request to the API would have been valid and worked.
I tried fixing this issue by simply updating moltin.request.Request#post() to always pass the payload as the json parameter instead of always passing it as the data parameter, but this caused the moltin.authenticate() function to fail. I think this happens because there's some code in the Moltin Python SDK that expects and relies on the payload being transformed into POST parameters instead of a JSON string.
I'm not experienced enough with Python or the code base of the Moltin Python SDK to see an easy fix for this issue but hopefully all this information will make it easy for one of the repository maintainers to fix it.
I've been getting an error response from the API (see https://github.com/moltin/issues/issues/22 for more info). After further testing I've found that the issue is being caused by the way the Moltin Python SDK is handling sending JSON payloads to the API.
In
moltin.request.Request#post()
the SDK is performing the call to the API using the Python Requests package by doing the following:The function it's calling is
requests.api.post(url, data=None, json=None, **kwargs)
, and the issue is that the Moltin Python SDK is always passing thedata
parameter and never passing thejson
parameter. This means that when the Requests library gets torequests.models.PreparedRequest#prepare_body()
it attempts to squash a payload that needs to be represented by JSON into POST parameters, which fails for nested JSON.For example, let's say I'm trying to perform the following with the Moltin Pyton SDK:
Due to
moltin.request.Request#post()
passing this payload torequests.api.post()
as thedata
parameter instead of thejson
parameter, therequests.models.PreparedRequest#prepare_body()
function will turn this payload into the following:Then the request to the API obviously fails since the
bill_to
provided to the API is completely invalid, and it returns a response of'billing_address': ["Address 'postcode' not found"]
.If the payload had instead been passed as the
json
parameter, therequests.models.PreparedRequest#prepare_body()
function would have turned this payload into a JSON string and set theContent-Type
header toapplication/json
, and the request to the API would have been valid and worked.I tried fixing this issue by simply updating
moltin.request.Request#post()
to always pass the payload as thejson
parameter instead of always passing it as thedata
parameter, but this caused themoltin.authenticate()
function to fail. I think this happens because there's some code in the Moltin Python SDK that expects and relies on the payload being transformed into POST parameters instead of a JSON string.I'm not experienced enough with Python or the code base of the Moltin Python SDK to see an easy fix for this issue but hopefully all this information will make it easy for one of the repository maintainers to fix it.