0

I have a GET and PUT request built below:

 from flask import Flask from flask_restful import Api, Resource, reqparse app = Flask(__name__) api = Api(app) userStorage =[ { "id": "1234", "currentBot": "BestBot" } ] class User(Resource): def get(self, id): for user in userStorage: if(id == user["id"]): return user, 200 return "User not found", 404 def put(self, id): parser = reqparse.RequestParser() parser.add_argument("currentBot") args = parser.parse_args() for user in userStorage: if(id == user["id"]): user["currentBot"] = args["currentBot"] return user, 200 user = { "id": id, "currentBot": args["currentBot"] } userStorage.append(user) return user, 201 def delete(self, id): global userStorage userStorage = [user for user in userStorage if user["id"] != id] return "{} is deleted.".format(id), 200 api.add_resource(User, "/user/<string:id>") app.run(debug = True, port = 4000) 

Postman can properly get a response 200 when I do a simple get request but when I try to do a request through my own program it returns a 404

import requests payload2Storage = { "currentBot": "BestBot" } headers = {"Content-Type": "application/json"} params = { "id": "1234" } #response = requests.request("PUT", "http://127.0.0.1:4000/user/", data=payload2Storage, params=params, headers=headers) response2 = requests.request("GET", "http://127.0.0.1:4000/user/", params=params, headers=headers) 

Is there something wrong with my request to get the info from userStorage?

1 Answer 1

1

In the client code, changing from 127.0.0.1 to localhost worked for me. Try this:

response2 = requests.request("GET", "http://localhost:4000/user/", params=params, headers=headers) 

OR in the server code, bind the server to 127.0.0.1 explicitly via host argument like this:

app.run(debug = True, port = 4000, host='127.0.0.1') 

Other error in the code is user["id"] returns a str while id is a int, change the code as below:

def get(self, id): for user in userStorage: if(id == int(user["id"])): return user, 200 return "User not found", 404 
Sign up to request clarification or add additional context in comments.

6 Comments

It still returns a 404. I noticed in the URL its creating a query string request so the url looks like this: /user/?id=1234 HTTP/1.1" 404 -. I noticed that if I just plugged in the id directly into the url without the "?id=" it actually works. Would this be the line that causes this? api.add_resource(User, "/user/<string:id>") (in original server code snippet)
do this change api.add_resource(User, "/user/<int:id>")
Still returning a 404? '127.0.0.1 - - [19/Jul/2018 11:57:40] "GET /user/?id=1234 HTTP/1.1" 404 -'
there is a discrepency here: In your server program you are expecting id as a Path Param i.e. api.add_resource(User, "/user/<int:id>") whereas from your request you are sending it as a Query Param and hence 404. Change your client to have this response2 = requests.request("GET", "http://localhost:4000/user/1234", params=params, headers=headers)
I see where you are coming from. however the id is the param. how do I tell it to put the id as a query param as the id is flexible and can change depending on the user being searched?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.