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':'[email protected]'}
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?
str
It looks like your API routes are returning raw string casted versions of the resource objects. You instead want to return them as JSON, which flask can do for you with the jsonify
method.
import flask
class New_Customer(Resource):
def put(self):
q = PHQuery()
data = request.get_json()
new_cust = q.create_cust(data)
return flask.jsonify(new_cust)
Notice how in Python, the string representation of a native dictionary is not exactly equivalent to the JSON version:
>>> dct = {'Hey': 'some value'}
>>> print(dct)
{'Hey': 'some value'}
>>> print(json.dumps(dct))
{"Hey": "some value"}
PUT/POST
When using the requests
library to send a PUT/POST request, you are by default sending the body of the request in a form-encoded (application/x-www-form-urlencoded
to be specific) format, which is explained here. This format is not great for nested structured data like what you are attempting to send. Instead, you can send the data JSON encoded (application/json
) using the json
keyword argument.
requests.put(url, json=data2)
Side note: If you are attempting to create a new customer, you may want to use POST instead as it may be considered more RESTful, as PUTs are generally for updating an existing resource, but this is up to you.
PUT/POST
DataSince we are sending the body of the request as JSON now, we need to deserialize it as such in the API server. You can see this happening above in the following line:
data = request.get_json()
You can refer to this answer for more information about properly pulling data out of a HTTP request in Flask.