D
D
DennyD3142017-03-29 22:34:49
Django
DennyD314, 2017-03-29 22:34:49

How to accept and parse json from payment aggregator api?

Hello!
I can’t solve the problem in any way, which consists in accepting a POST request from the JSON aggregator API and parsing it.
I do this:
I send the following request via CURL:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d "{'json':{'data':'here'}}" 127.0.0.1:8000/check/

The representation is this (broken into parts to look in the debug):
one = request.body
        dump = json.dumps(one) // Без этого шага лоадс падает с ошибкой, об отсутствии двойных кавычек
        val = json.loads(dump)
        jsd = val[data]

Further, the error string indices must be integers I
have already looked at half the Internet, and, perhaps, two-thirds of Stackoverflow, I just can’t figure it out.
So how do you still work with json received from a POST request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FeNUMe, 2017-03-30
@DennyD314

Pass valid json as input (with double quotes) and the code will work for you:

val = json.loads(request.body)
print(val['json']['data'])

json.dumps() doesn't fix the quotes, it just serializes the incoming object (in your case, a string).
If there is no way to fix the incoming format, then you can do this
val = ast.literal_eval(request.body.decode('utf-8'))
print(val['json']['data'])

A
artem78, 2017-03-30
@artem78

You can do this:

import requests

json = {'json':{'data':'here'}}
r = requests.post('http://127.0.0.1:8000/check/', json=json)
res = r.json()
print(res)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question