I am using this documentation: from FLASK Restful
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
todos = {}
class TodoSimple(Resource):
def get(self, todo_id):
return {todo_id: todos[todo_id]}
def put(self, todo_id):
todos[todo_id] = request.form['data']
return {todo_id: todos[todo_id]}
api.add_resource(TodoSimple, '/<string:todo_id>')
if __name__ == '__main__':
app.run(debug=True)
In order to add the new todo, they call the following command on python:
from requests import put, get
put('http://localhost:5000/todo1', data={'data': 'Remember the milk'}).json()
The data comes from the json object {'data': '.....}
and it retrieved here: request.form['data']
I want to replace 'Remember the milk' with my data2 object:
data2 = {'ALTNUM':'noalt', 'CUSTTYPE': 'O',
'LASTNAME':'lnamedata2', 'FIRSTNAME':'fndata2', 'ADDR':'1254 data 2 address',
'ADDR2':'apt 3', 'CITY':'los angeles',
'COUNTY':'011', 'STATE':'CA',
'ZIPCODE':'90293', 'COUNTRY':'001',
'ADDR_TYPE':'O','PHONE':'4254658029',
'PHONE2':'3442567777',
'EMAIL':'test2@test2.com'}
and call: print put('http://localhost:5000/newcustomer', data={'data':data2}).json()
MY API resource:
class New_Customer(Resource):
def put(self):
q = PHQuery()
data = request.form['data']
print(data)
return data
I get the following errors when calling these:
print put('http://localhost:5000/newcustomer', data=data2).json()
print put('http://localhost:5000/newcustomer', data={'data':data2}).json()
print put('http://localhost:5000/newcustomer', data=data2).json()
print put('http://localhost:5000/newcustomer', data=data2)
{u'message': u'The browser (or proxy) sent a request that this server could not understand.'}
CUSTTYPE
{u'message': u'The browser (or proxy) sent a request that this server could not understand.'}
<Response [400]>
What am I doing wrong? @foslock should it be data = request.json['data'] ? since it is an object and not form data?