I'm testing the iugu.com api to generate ticket payments and I'm having problems with json's return because there is no error in dictionary items which is sent to the API (in php 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 site that looks like this:
'items[][description]':'Item Um',
'items[][quantity]':'1',
'items[][price_cents]':'100000',
The response returns correctly, but you can not add more than one item because it only considers the second entry. Is there another way to send data or am I doing something wrong?