1
I’m testing the api of yugu. to generate boletus payments and I’m having problems returning json because there is no error in Dictionary items that is sent to the API (in php it would be an array)
the code is as follows::
from flask import Flask, Response, jsonify
import requests
from requests.auth import HTTPBasicAuth
import json
app = Flask(__name__)
@app.route("/")
def index():
dados = {
'method':'bank_slip',
'email':'[email protected]',
'items[]':[
{'description':'Item Um', 'quantity':1, 'price_cents': 100000},
{'description':'Item Dois', 'quantity':1, 'price_cents': 200000}
]
}
url = 'https://api.iugu.com/v1/charge'
user = '002f38a6d40b0275fc08aaac503a775b'
headers = {'content-type': 'application/json'}
r = requests.post(url, auth=HTTPBasicAuth(user, ''), headers = headers, params = json.dumps(dados))
return jsonify(
resposta = [
r.json(),
{
'status': r.status_code,
'cabecalho': r.headers['Content-Type'],
'params': dados,
'text': r.content,
'url': r.url
}
])
app.debug = True
app.use_reloader=True
app.run()
The answer:
{
"resposta": [
{
"errors": "token n\u00e3o \u00e9 v\u00e1lido"
},
{
"cabecalho": "application/json; charset=utf-8",
"params": {
"email": "[email protected]",
"items[]": [
{
"description": "Item Um",
"price_cents": 100000,
"quantity": 1
},
{
"description": "Item Dois",
"price_cents": 200000,
"quantity": 1
}
],
"method": "bank_slip"
},
"status": 400,
"text": "{\"errors\":\"token n\\u00e3o \\u00e9 v\\u00e1lido\"}",
"url": "https://api.iugu.com/v1/charge?%7B%22email%22:%20%[email protected]%22,%20%22items[]%22:%20[%7B%22price_cents%22:%20100000,%20%22description%22:%20%22Item%20Um%22,%20%22quantity%22:%201%7D,%20%7B%22price_cents%22:%20200000,%20%22description%22:%20%22Item%20Dois%22,%20%22quantity%22:%201%7D],%20%22method%22:%20%22bank_slip%22,%20%22format%22:%20%22json%22%7D"
}
]
}
If I try to use as in the example of the site that looks like this:
'items[][description]':'Item Um',
'items[][quantity]':'1',
'items[][price_cents]':'100000',
the answer returns correctly, but it is not possible to add more than one item because it only considers the second insertion. Is there another way to send data or am I doing something wrong?
I think this might be a hand on the wheel for you https://github.com/iugu/iugu-python
– Paulo