Request body malformed

Hi,

What can be wrong here? When I run this code in Python, I get <Response [403]> , “{“Status”:“Error”,“Message”:“Request body malformed”,“Meta”:null}”

tyk = requests.Session()

resp = tyk.get(dashboardURL + “/api/portal/policies/search?q=” + params[“plan”], headers = {‘authorization’: app.config[‘TYK_API_KEY’]}).json()

key_request = {
“by_user” : developer[‘id’], #5b5c997b04a4f40001875006
‘date_created’ : datetime.datetime.today().isoformat(), #2018-08-01T00:11:26.810696
“for_plan” : resp[“Data”][0]["_id"], #5b6130af49e9990001f5d6dc
“version” : “v2”
}

resp = tyk.post(dashboardURL + “/api/portal/requests”, data = json.dumps(key_request), headers = {‘authorization’: app.config[‘TYK_API_KEY’]}) # fails here

Hi

date_created only has single quotes

Also i think you want to wrap the authorization header in the quotes with its keys so headers = {‘authorization: app.config[‘TYK_API_KEY’]’}

In Python, single and double quotes are the same. Also,

headers = {‘authorization’: app.config[‘TYK_API_KEY’]}

is correct: it works in all other requests I make. As you can see it also works in the GET request in the same example in my original post:

resp = tyk.get(dashboardURL + “/api/portal/policies/search?q=” + params[“plan”], headers = {‘authorization’: app.config[‘TYK_API_KEY’]}).json()

Can you please validate on your backend what went wrong? On your backend, my request will look like this:

POST URL
https://admin.cloud.tyk.io/api/portal/requests

Payload:
{“date_created”: “2018-08-01T00:11:26.810696”, “version”: “v2”, “by_user”: “5b5c997b04a4f40001875006”, “for_plan”: “5b6130af49e9990001f5d6dc”}

The request comes with the “authorization” header containing my API key.

I can email you my API key if needed.

Actually, the problem was with the date format. Python’s datetime.datetime.today().isoformat() generates the string that your API rejects: “2018-08-01T00:11:26.810696”. Your API needs a string like this “2018-08-01T15:49:20.992-04:00” but there are no means to generate a string like this in Python, so I generate the prefix “2018-08-01” and fake the suffix so it looks like this “T15:49:20.992-04:00”. Not the best solution, but I have not found a better one.