Python Flask Restful API - python

Part of my Flask code:
#app.route('/api/post', methods=['POST'])
def post():
body = request.get_json()
json_body = json.loads(body)
new_id = mongo.db.Projects.insert(json_body)
return str(new_id)
Script to post a new database entry:
payload = { 'ProjectName' : 'KdB Test Project' }
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
r = requests.post('http://localhost:5000/api/post', headers=headers, data=json.dumps(payload))
I keep getting json decoder TypeError problems, e.g.
TypeError: expected string or buffer
2016-08-16 15:19:31,388 - werkzeug - INFO - 127.0.0.1 - - [16/Aug/2016 15:19:31] "POST /api/post HTTP/1.1" 500 -
I have tried several things, incl. posting strings. Any clue what is wrong with the way I post a dictionary?
The problem appears to be at the point of body = request.get_json(). I don't think I am picking up any data...

You don't need to loads request message to get dict format. Information stored in body variable is already in dict form. Simply doing the following should remove the error:
#app.route('/api/post', methods=['POST'])
def post():
body = request.get_json()
# json_body = json.loads(body)
new_id = mongo.db.Projects.insert( body )
return str( new_id )

http://flask.pocoo.org/docs/0.11/api/#flask.Request.get_json
get_json is probably returning a dict already, so you don't need to call json.loads(body) (this is what most likely causes the TypeError).
#app.route('/api/post', methods=['POST'])
def post():
json_body = request.get_json()
new_id = mongo.db.Projects.insert(json_body)
return str(new_id)

Related

What is the best method to return smmry api request with block of text in json instead of url?

I am trying to write a function in python that returns the json from a request to the smmry API. I was able to get it working with the SM_URL request like this:
def summry():
API_ENDPOINT = "https://api.smmry.com"
API_KEY = "B..."
params = {
"SM_API_KEY":API_KEY,
"SM_URL":"https:..."
}
r = requests.get(url=API_ENDPOINT, params=params)
return r.json()
However, I am not sure how you would do this for passing in a block of text instead of a URL. I have tried making the request with sm_api_input=my_input but that returned an error of insufficient variables. I have also tried it with a POST request and got the same error.
If anyone is curious, this is how I solved the problem. Turns out I needed an Expect: 100-continue header and the sm_api_input is a separate post field instead of a get query.
def summry(text):
API_KEY = "B..."
API_ENDPOINT = "https://api.smmry.com"
data = {
"sm_api_input":text
}
params = {
"SM_API_KEY":API_KEY
}
header_params = {"Expect":"100-continue"}
r = requests.post(url=API_ENDPOINT, params=params, data=data, headers=header_params)
return r.json()

Django view doesn't receive Json data?

Using a simple Python script, i want to send a request, with Python-Requests, to a Django view. The Django view should receive the json data inside the request and should print it to my console; here is what i tried:
This is how i send the request:
url = 'http://127.0.0.1:8000/myview/view'
client = requests.session()
csrftoken = requests.get(url).cookies['csrftoken']
data = json.dumps({'data': 'test-value'})
header = {'X-CSRFToken': csrftoken}
cookies = {'csrftoken': csrftoken}
resp = requests.post(url, data=data, headers=header, cookies=cookies)
And this is how the Django view receives it:
def myview(request):
if request.method == 'POST':
data = request.POST.get('data')
print(data)
print('received.')
response = HttpResponse(get_token(request))
return response
The problem with my current code is that print(data) will throw the following output:
None
received.
[06/Jan/2020 21:23:57] "POST /myview/view HTTP/1.1" 200 64
So, instead of printing test-value, it prints nothing. I don't understand whether the error is in my Django view or in how i'm sending the request. Any advice is appreciated!
The problem is with your request, and entirely caused by this line:
data = json.dumps({'data': 'test-value'})
You simply want
data = {'data': 'test-value'}
The POST data should be sent as a simple dictionary, not a JSON string - see the documentation and example here.

Requests.post error| TypeError: post() takes at least 1 argument (1 given)

I'm using Python 2.7.10 64-bit.
In the update_jira_field method, I'm getting the following error:
TypeError: post() takes at least 1 argument (1 given)
I tried also requests.put(), combination of json = payload while declaring the payload as a json, but still got the same error.
I'm not sure what am I doing wrong, never experienced this error while using the requests module.
import requests
import json
import urllib2
auth = *****
propertKey = 'customfield_13557'
headers = {'Accept':'application/json','Bearer':****'}
def get_jira_real_id(jiraKey):
endpoint = 'https://****.atlassian.net/rest/api/3/issue/{0}'.format(jiraKey)
response = requests.get(endpoint, headers = headers, auth = auth)
if response.status_code == 200:
print "Success getting Jira Id"
response = json.loads(response.text)
return response['id']
def update_jira_field(jiraId,jiraKey):
endpoint = 'https://****.atlassian.net/rest/api/3/issue/{0}'.format(jiraId)
payload = dict({"fields": {"customfield_13557":{"self": "https://****.atlassian.net/rest/api/3/customFieldOption/14915", "value": "Yes", "id": "14915"}}})
response = requests.post(endpoint = endpoint, headers = headers, auth = auth, data = payload)
if response.status_code == 200:
print "Success! Updated", jiraId, jiraKey
jiraList = ['****']
for jiraKey in jiraList:
jiraId = get_jira_real_id(jiraKey)
update_jira_field(jiraId, jiraKey)
print "Done Done Done"
Any idea why I get this error? and how do I fix it?
You try to pass in a named parameter named endpoint, but the correct name is url. It whould work if you change the line to
response = requests.post(endpoint, headers = headers, auth = auth, data = payload)

Error:how to fix the swift error type to my server

I get a confused error.
Its about swift data type.
there is my network demo:
func initRequst(HttpType: httpType,content:String,dic:Dictionary<String,Any>){
let data = try! JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
print(data)
switch HttpType {
case .post:
url = URL.init(string: "http://localhost:5000/" + content)
req = NSMutableURLRequest.init(url: url!)
req?.httpMethod = "POST"
req?.httpBody = data
case .get:
url = URL.init(string: "http://localhost:5000/" + content)
req = NSMutableURLRequest.init(url: url!)
req?.httpBody = data
req?.httpMethod = "GET"
}
}
And there is my flask server have received error and demo:
TypeError: 'NoneType' object is not subscriptable
#app.route('/userLogin',methods=["GET"])
def userLogin():
get_username = request.json['username']
get_password = request.json['password']
return jsonify({"uerId":databasePort.checkUserInformation(get_username,get_password)})
it seems that swift data sending type have not be identified.How to fix it? Thanks!
You could try setting the Content-Type header to application/json before sending the request in Swift. Then Flask will attempt to decode it and assign it to the request's json attribute:
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
Flask will set request.json only if the incoming request sets the content type to json, otherwise it will assume form encoded data which it will assign to request.form.

Return a requests.Response object from Flask

I'm trying to build a simple proxy using Flask and requests. The code is as follows:
#app.route('/es/<string:index>/<string:type>/<string:id>',
methods=['GET', 'POST', 'PUT']):
def es(index, type, id):
elasticsearch = find_out_where_elasticsearch_lives()
# also handle some authentication
url = '%s%s%s%s' % (elasticsearch, index, type, id)
esreq = requests.Request(method=request.method, url=url,
headers=request.headers, data=request.data)
resp = requests.Session().send(esreq.prepare())
return resp.text
This works, except that it loses the status code from Elasticsearch. I tried returning resp (a requests.models.Response) directly, but this fails with
TypeError: 'Response' object is not callable
Is there another, simple, way to return a requests.models.Response from Flask?
Ok, found it:
If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers). The status value will override the status code and headers can be a list or dictionary of additional header values.
(Flask docs.)
So
return (resp.text, resp.status_code, resp.headers.items())
seems to do the trick.
Using text or content property of the Response object will not work if the server returns encoded data (such as content-encoding: gzip) and you return the headers unchanged. This happens because text and content have been decoded, so there will be a mismatch between the header-reported encoding and the actual encoding.
According to the documentation:
In the rare case that you’d like to get the raw socket response from the server, you can access r.raw. If you want to do this, make sure you set stream=True in your initial request.
and
Response.raw is a raw stream of bytes – it does not transform the response content.
So, the following works for gzipped data too:
esreq = requests.Request(method=request.method, url=url,
headers=request.headers, data=request.data)
resp = requests.Session().send(esreq.prepare(), stream=True)
return resp.raw.read(), resp.status_code, resp.headers.items()
If you use a shortcut method such as get, it's just:
resp = requests.get(url, stream=True)
return resp.raw.read(), resp.status_code, resp.headers.items()
Flask can return an object of type flask.wrappers.Response.
You can create one of these from your requests.models.Response object r like this:
from flask import Response
return Response(
response=r.reason,
status=r.status_code,
headers=dict(r.headers)
)
I ran into the same scenario, except that in my case my requests.models.Response contained an attachment. This is how I got it to work:
return send_file(BytesIO(result.content), mimetype=result.headers['Content-Type'], as_attachment=True)
My use case is to call another API in my own Flask API. I'm just propagating unsuccessful requests.get calls through my Flask response. Here's my successful approach:
headers = {
'Authorization': 'Bearer Muh Token'
}
try:
response = requests.get(
'{domain}/users/{id}'\
.format(domain=USERS_API_URL, id=hit['id']),
headers=headers)
response.raise_for_status()
except HTTPError as err:
logging.error(err)
flask.abort(flask.Response(response=response.content, status=response.status_code, headers=response.headers.items()))

Categories