7

http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flas...

 1 year ago
source link: https://gist.github.com/miguelgrinberg/5614326
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The code from my article on building RESTful web services with Python and the Flask microframework. See the article here

Instantly share code, notes, and snippets.

The code from my article on building RESTful web services with Python and the Flask microframework. See the article here: http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask

How to pass credentials using post like username in get method

@patilanup246: credentials are passed in the same way for GET and POST requests.

I mean using a postman Google extension

@patilanup246: use the options provided by Postman for HTTP Basic Authentication.

Thanks Miguel, this was very helpful. Just what i was looking for, well explained and easy to understand.

@ineam how to solve that Unicode error ?

@sskanishk You can just replace it with str in python 3+
if 'title' in request.json and type(request.json['title']) != str:
abort(400)
if 'description' in request.json and type(request.json['description']) is not str:
abort(400)

@sskanishk You can just replace it with str in python 3+
if 'title' in request.json and type(request.json['title']) != str:
abort(400)
if 'description' in request.json and type(request.json['description']) is not str:
abort(400)

solved; thanks

Copied the code verbatim. But after curl -u miguel:python -i http://localhost:5000/todo/api/v1.0/tasks
I get the error message:

raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <map object at 0x10a405160> is not JSON serializable

Thank you!
Great tutorial!!

Thank you so much, Miguel, for this wonderful code. Do you happen to have any advice or examples for anyone writing a client for a server like this?

@ronk7 for a JavaScript client I wrote a follow-up tutorial: https://blog.miguelgrinberg.com/post/writing-a-javascript-rest-client. For a Python client use the requests package.

ronk7

commented

Jul 20, 2020

via email

Sorry - what requests package?

@ronk7 pip install requests

Hey @robinvd, I fixed the issue by wrapping map with list:

return jsonify( { 'tasks': list(map(make_public_task, tasks)) } )

And the same happens with filter:

task = list(filter(lambda t: t['id'] == task_id, tasks))

The fixes are needed for Python3.

Hello,Miguel, sorry for my question about it. I'm beginner in flask , true, I'am front-end developer , but i need learn Flask and write Rest server for my task. But i have error while i copy-paste your code.
If a write request curl -i http://localhost:5000/todo/api/v1.0/tasks/2
I getting Error in JSON , why? Can u help me.
See you soon

@CPPtrueCoder What's the error? You don't have any JSON, so what do you mean by "error in JSON"?

When i get curl -i http://localhost:5000/todo/api/v1.0/tasks/2
From server response {
"error": "Not found"
}
but tasks is available :(

Author

@CPPtrueCoder How do you know it is available? What is the response when you send a request to http://localhost:5000/todo/api/v1.0/tasks?

if request : http://localhost:5000/todo/api/v1.0/tasks then tasks like your guide , but then i try request with ID , i have troubles

image

(venv) C:\Users\a.rodin\PycharmProjects\flaskProject>curl -i http://localhost:5000/todo/api/v1.0/tasks/1
HTTP/1.0 404 NOT FOUND
Content-Type: text/html; charset=utf-8
Content-Length: 232
Server: Werkzeug/1.0.1 Python/3.8.8
Date: Tue, 11 May 2021 10:02:55 GMT

<title>404 Not Found</title>

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

i tried rewrite it according updates from comments , but no result , unfortunately

Lol, i changed function :
@app.route('/todo/api/v1.0/tasks/<task_id>', methods=['GET']), instead @app.route('/todo/api/v1.0/tasks/int:task_id', methods=['GET'])
And it's working , but i think it isnt' safely, isn't it ?

@CPPtrueCoder you should use the code as shown above. That is the best. Neither of the two options you posted above matches my code.

Thank you. exactly what i needed as a complete beginner

super useful and straightforward... thanks!!

Thanks, this is helpful, but I do not get the id in the response:

~ curl -u username:password -i http://localhost:5000/todo/api/v1.0/tasks
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 407
Server: Werkzeug/2.0.2 Python/3.8.9
Date: Tue, 21 Dec 2021 01:34:46 GMT

{
  "tasks": [
    {
      "description": "Milk, Cheese, Pizza, Fruit, Tylenol",
      "done": false,
      "title": "Buy groceries",
      "uri": "http://localhost:5000/todo/api/v1.0/tasks/1"
    },
    {
      "description": "Need to find a good Python tutorial on the web",
      "done": false,
      "title": "Learn Python",
      "uri": "http://localhost:5000/todo/api/v1.0/tasks/2"
    }
  ]
}

And so I am unable to DELETE it either:

line 102, in delete_task
    if len(task) == 0:
TypeError: object of type 'filter' has no len()

@arafatmohammed you are obviously using different code than what I posted here, so I have no way to tell you what's wrong. Try using my code exactly as shown here.

@miguelgrinberg Thnks for ur great job.
I think you'd better rename this function name to sth better as it conflicts with this function name.
I suggest using def bad_request(error) instead.

Awesome step by step tutorial, Thank you miguel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK