DEV Community

Cover image for Flask Rest API -Part:0- Setup & Basic CRUD API

Flask Rest API -Part:0- Setup & Basic CRUD API

Paurakh Sharma Humagain on December 10, 2019

Part 0: Setup & Basic CRUD API Howdy! Welcome to the Flask Rest API - Zero to Yoda, tutorial series. We will go through building a M...
Collapse
 
dilless profile image
Huangbo

Thanks for the tutorial!
There may be some error codes here:

+@app.route('/movies', methods=['POST'])
+def add_movie():
+    movie = request.get_json()
+    movies.append(movie)
+    return {'id': len(movies)}, 200
Enter fullscreen mode Exit fullscreen mode

The add_movie() function return the length of movies as the last id, so the first get request should return id: 3 rather than id: 2 (in the image of post response). And in other function, use index to access movie. So, I think should change return {'id': len(movies), 200 to return {'id': len(movies) - 1, 200 in the code.
Sorry for my poor english.

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Yes, that's right. I never realized it. Thanks for pointing it out.
Also, your English is good :)

I will update the article shortly.

Collapse
 
jvmazagao profile image
João Victor

I want to point another thing here, the response code should be 201 because we are creating an object, or Am I wrong?

Collapse
 
varuntumbe profile image
Varun Hegde

I am having trouble installing pipenv. i added path variable also and now i cant run pipenv shell command. Can i do this using venv package management ?
(pls correct me if i am wrong , newbie here).
Thanks in advance

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Yes sure, just two ways of doing same thing ;)

Collapse
 
varuntumbe profile image
Varun Hegde

And one last thing when i import db from .db in model.py i get this error
-Attempted relative import beyond top level package .(its given by pylint in vscode).

Collapse
 
sph73 profile image
SPH73 • Edited

Hi! Is there any chance someone can see what I am doing wrong? I can do get and post requests but put and delete give a 405 error. I have uploaded a screenshot. TIA!
Edit:
Don't know why the screenshot isn't showing and I can't even paste into this box. :(

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Hey, I am not sure why you are having this problem.
You should be able to upload the screenshot using upload image icon.
If for some reason you cannot. You can upload your screenshot somewhere and paste the link here.
Or maybe your project GitHub URL.

Collapse
 
beddiafmeriem profile image
BeddiafMeriem

it's maybe because you forgot to specify the index in your put or delete request "localhost/movies/1"

Collapse
 
sph73 profile image
SPH73

Hi, thanks yes I didn't specify an index, so that might be it.

Collapse
 
israelias profile image
Joem Elias Sanez

I just stumbled upon this three and a half years later. Alas, my comment and me meet again! I owe this seven part series my future. Passing by and paying dues? It deserves a Google review. This tutorial was foundational.

Collapse
 
lukefirstofhisname profile image
luke

I basically copied the code from the tutorial.
when I try to POST using Postman, I get the following error
TypeError: TopLevelDocumentMetaclass object argument after ** must be a mapping, not NoneType
127.0.0.1 - - [17/Oct/2020 16:26:32] "POST /movies HTTP/1.1" 500 -

Collapse
 
purpetrator profile image
purpetrator

Absolutely love this tutorial series, thank you SO much!

One quick little typo I noticed that may throw off some beginners - you call the GET request on localhost:5000/ instead of localhost:5000/movies in postman

Collapse
 
lukasmari profile image
Lukas Marivoet

Super tutorial, a really practical walkthrough of working with Flask. Awesome job!

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

I am glad that you liked it 🙂

Collapse
 
corsibu profile image
corsibu

Hi, thanks for the tutorial, I enjoyed going through it. Do you think you could add another post about testing?

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

I am glad that you liked it.
I am planning to write about exception handling next and then testing :)

Collapse
 
just012 profile image
Just Basumatary

Thanks for the tutorial mate. It really helped me in understanding the working of Flask API.

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

Thanks for the tutorial

Collapse
 
carrenolg profile image
Luis Giovanny Carreño Ortiz • Edited

If you stuck at any part, you can check this repo.

Collapse
 
jhaabhinav27 profile image
Abhinav jha • Edited

I am getting null output when I send the get request

Collapse
 
kowalflecha profile image
Francisco Soster

Nice tutorial! Thank you

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

I am glad that you liked it.
Please let me know if you have any suggestions
and also if you run into any issues while following
the series.

Collapse
 
wendelrios profile image
wendel

Ii'm getting a 'method not allowed' in the post method. i searched for answers but none of them solve my problem. how go through it?

Collapse
 
israelias profile image
Joem Elias Sanez

same. just restart app.py