Instantly share code, notes, and snippets.

Embed
What would you like to do?
from flask import Flask
from flask_restful import Api, Resource, reqparse
app = Flask(__name__)
api = Api(app)
users = [
{
"name": "Nicholas",
"age": 42,
"occupation": "Network Engineer"
},
{
"name": "Elvin",
"age": 32,
"occupation": "Doctor"
},
{
"name": "Jass",
"age": 22,
"occupation": "Web Developer"
}
]
class User(Resource):
def get(self, name):
for user in users:
if(name == user["name"]):
return user, 200
return "User not found", 404
def post(self, name):
parser = reqparse.RequestParser()
parser.add_argument("age")
parser.add_argument("occupation")
args = parser.parse_args()
for user in users:
if(name == user["name"]):
return "User with name {} already exists".format(name), 400
user = {
"name": name,
"age": args["age"],
"occupation": args["occupation"]
}
users.append(user)
return user, 201
def put(self, name):
parser = reqparse.RequestParser()
parser.add_argument("age")
parser.add_argument("occupation")
args = parser.parse_args()
for user in users:
if(name == user["name"]):
user["age"] = args["age"]
user["occupation"] = args["occupation"]
return user, 200
user = {
"name": name,
"age": args["age"],
"occupation": args["occupation"]
}
users.append(user)
return user, 201
def delete(self, name):
global users
users = [user for user in users if user["name"] != name]
return "{} is deleted.".format(name), 200
api.add_resource(User, "/user/<string:name>")
app.run(debug=True)
@KaranNayak

This comment has been minimized.

KaranNayak commented Sep 20, 2018

I get 404 page not found error, please help me

@joannawheeler

This comment has been minimized.

joannawheeler commented Oct 10, 2018

I get 404 page not found error, please help me

Is it because you forgot to add the name to your url?

For an example of doing a get request for a user named Nicholas, you will need to write http://127.0.0.1:5000/user/Nicholas instead of just http://127.0.0.1:5000. The need for the name string is specified here:

 api.add_resource(User, "/user/<string:name>")

expink

@romelrkhan

This comment has been minimized.

romelrkhan commented Oct 29, 2018

I am getting an exception as shown below. I am using Anaconda with Python 3.6 on windows 10 pro. (I have no issue running this on linux by the way.)

  • Restarting with stat
    An exception has occurred, use %tb to see the full traceback.

SystemExit: 1


Here is with the %tb:::
%tb
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/ctom/Documents/Python Scripts/temp3.py', wdir='C:/Users/ctom/Documents/Python Scripts')

File "C:\Users\ctom\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\ctom\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/ctom/Documents/Python Scripts/temp3.py", line 77, in
app.run(debug=True)

File "C:\Users\ctom\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 841, in run
run_simple(host, port, self, **options)

File "C:\Users\ctom\AppData\Local\Continuum\anaconda3\lib\site-packages\werkzeug\serving.py", line 812, in run_simple
reloader_type)

File "C:\Users\ctom\AppData\Local\Continuum\anaconda3\lib\site-packages\werkzeug_reloader.py", line 275, in run_with_reloader
sys.exit(reloader.restart_with_reloader())

SystemExit: 1


code in _reloader.py is generating the system exit in function run_with_reloader. Snippet of code is shown below:::
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
t = threading.Thread(target=main_func, args=())
t.setDaemon(True)
t.start()
reloader.run()
else:
sys.exit(reloader.restart_with_reloader())

My environment does not have 'WERKZEUG_RUN_MAIN' as a key though the flask install did not face any issue. So what should have set that key? Does this have to be manually set in windows for anaconda install?
The code actually runs properly with Anaconda when 'debug=True' is removed in app.run(debug=True).

@rkohardy

This comment has been minimized.

rkohardy commented Nov 12, 2018

http://127.0.0.1:5000/user/Nicholas
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

@dhruv14-4

This comment has been minimized.

dhruv14-4 commented Nov 16, 2018

Why is it taking a GET request. Its nowhere specified in the code to take default GET function
How can we change it to another function(ex:POST)

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