Get Data JSON in Flask

2024/9/20 19:56:31

Even following many example here & there, i can't get my API work in POST Method. Here the code about it :

from flask import Flask, jsonify, request@app.route('/api/v1/lists', methods=['POST'])
def add_entry():print("p0")content = request.get_json()appname = content.get('title')print(content)print(appname)

When i query with curl (i'm running it on Windows):

curl.exe -i -H "Content-Type: application/json" -X POST -d '{"title":"titi"}' http://localhost:5000/api/v1/lists

curl.exe -i -H "Content-Type: application/json" -X POST -d "{"""title""":"""Read a book"""}" http://localhost:5000/api/v1/lists

I have always a 400 error in return:

HTTP/1.0 400 BAD REQUEST Content-Type: text/html Content-Length: 192 Server: Werkzeug/0.12.1 Python/3.6.0 Date: Tue, 04 Apr 2017 21:55:29 GMT400 Bad Request Bad Request The browser (or proxy) sent a request that this server could not understand.

I dont see where the error is.

Thanks for your help.

Answer

Even with the "working" code, in case of your if-block failure (when value1 and value2 is None) will cause the same error, since you are not handling the else part of the if-block. The corrected should be:

@app.route('/api/v1/list', methods=['POST'])
def add_entry():print("p0")request_json     = request.get_json()value1           = request_json.get('First_Name')value2           = request_json.get('Last_Name')response_content = Noneif value1 is not None and value2 is not None:print("p3")cursor.execute("INSERT INTO person (first_name,last_name) VALUES (%s,%s)", (value1, value2))response_content = conn.commit()return jsonify(response_content)

Of course you may want something better than None as the response.

https://en.xdnf.cn/q/72136.html

Related Q&A

Commands working on windows command line but not in Git Bash terminal

I am trying to run certain commands in Git Bash but they continue to hang and not display anything. When I run them in the Windows command prompt they work.For example, in my windows command prompt the…

RBF interpolation: LinAlgError: singular matrix

The following call:rbf = Rbf(points[0], points[1], values,epsilon=2)results in an error:LinAlgError: singular matrixwith the following values:In [3]: points Out[3]: (array([71, 50, 48, 84, 71, 74, 89,…

What does `\x1b(B` do?

Im a Blessed user, and recently, when I tried to find out the contents of the term.bold() function, I got this output: \x1b[1m\x1b(B\x1b[mI understand what \x1b[1m and \x1b[m do, but what does \x1b(B d…

Not clicking all tabs and not looping once issues

I am trying to click the tabs on the webpage as seen below. Unfortunately, it only seems to click some of the tabs despite correct correct xpath in inspect Chrome. I can only assume it’s not clickin…

Opencv stream from a camera connected to a remote machine

I am developing a wx application in python for streaming and displaying video from two different webcams. This works fine, but now I need to do this in a different scenario in which the two cameras are…

Is there a callable equivalent to f-string syntax?

Everybody loves Python 3.6s new f-strings:In [33]: foo = {blah: bang}In [34]: bar = blahIn [35]: f{foo[bar]} Out[35]: bangHowever, while functionally very similar, they dont have the exact same semanti…

Python: list comprehension based on previous value? [duplicate]

This question already has answers here:Python list comprehension - access last created element(9 answers)Closed 10 months ago.Say I want to create a list using list comprehension like:l = [100., 50., 2…

How to run a coroutine inside a context?

In the Python docs about Context Vars a Context::run method is described to enable executing a callable inside a context so changes that the callable perform to the context are contained inside the cop…

Random Forest interpretation in scikit-learn

I am using scikit-learns Random Forest Regressor to fit a random forest regressor on a dataset. Is it possible to interpret the output in a format where I can then implement the model fit without using…

Why are three apostrophes needed for print in Python?

Im making this Pythagoras Theorem Calculator in Python 3.3.2.I made print over several lines so that I could make a diagram:print("Welcome to the Pythagoras Theorem Calculator, powered by Python!&…