0

I have a decently intermediate understanding of APIs, and am not trying to host information I've got from APIs onto the web.

This incredibly simple code:

from flask import Flask, request, render_template import requests app = Flask(__name__) @app.route('/temperature', methods=['POST']) def temperature(): zipcode = request.form['zip'] r = requests.get('http://api.openweathermap.org/data/2.5/weather?zip=' +zipcode+',us&appid=9b56b06ab4c7f06821ccf55e3e10fce5') json_obj = r.text return json_obj @app.route('/') def index(): return 'hi' if __name__ == '__main__': app.run(debug=True) 

Is not giving me anything (except the 405 error).

Can someone please explain why my POST method is not working?

6
  • What exactly do you do to get 405? How do you test this? Commented Dec 19, 2016 at 19:53
  • Hey! I go to 127.0.0.1:5000/temperature Commented Dec 19, 2016 at 19:55
  • When you browse to a page, it uses GET. You need to write test code to use POST. For example requests.post('http://127.0.0.1:5000/temperature', {'zip': '12345'}) Commented Dec 19, 2016 at 19:56
  • Thanks for your update on the question, but you seem to have only included the first few lines of the server output. Please show us the log when you actually request the /temperature page. Commented Dec 19, 2016 at 20:46
  • It was very, very confusing to read your question after you had essentially changed its nature. The 405 would occur with the code you currently have and it wouldn't with your new code. If you have a different question, please ask a different question; however, I strongly encourage you to work through the main issue you're having after you've successfully resolved this part. Commented Dec 19, 2016 at 20:52

2 Answers 2

1

While I'm not entirely sure what you have done, here's what I think you did:

You accessed the /temperature page in your browser which is by default a GET-Request.

You then get returned a 405 Method Not Allowed Error, because your route explicitly requires that the page will be accessed via the HTTP-POST Method.

Modify @app.route('/temperature', methods=['POST']) to @app.route('/temperature', methods=['POST', 'GET']) and you should be fine.

Sign up to request clarification or add additional context in comments.

7 Comments

Bad Request The browser (or proxy) sent a request that this server could not understand.
You get that error after you changed the line and reloaded the page? Please make sure your server reloaded the code as well.
yes, I do get this error when I change the code and reload the page. I even run it as python script_name.py
Can you please edit your question and show me what you have changed? Please include the relevant lines from your servers' log as well. This change should work in principle, maybe the call to the external API returns the error, which is passed on by flask...
@Maurice: Please don't suggest that the OP wantonly change their question in this fashion, as it genuinely invalidates your answer.
|
0

I'll post my answer from /r/ flask here, just for posterity.

the line in your code: zipcode = request.form['zip'] implies that you are submitting data from an HTML form to the view via a POST.

If there is no form, then request.form['zip'] does nothing, and is most likely raising the error. Considering that I see no route in your logic there that points to any HTML, I'm guessing that's the issue.

If you have not built a form for the user to submit the zip code, then you could build it into the route logic. For example, you could do:

@app.route('/temperature/<zipcode>') def temperature(zipcode): # add some kind of validation here to make sure it's a valid zipcode r = requests.get('http://api.openweathermap.org/data/2.5/weather?zip=' +zipcode+',us&appid=DONT_SHARE_YOUR_API_KEY') json_obj = r.text return json_obj 

You could then use http://127.0.0.1:5000/temperature/20003 to see the data for that zip code.

(as a side note, it's often unwise to publicly share your API keys. you may want to generate a new key.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.