Skip to content Skip to sidebar Skip to footer

Changing Get To Post In Python (flask)

I am trying to create a simple app where an array of integers is generated on the server and sent to the client. Here is some sample (working) code in app.py: from flask import Fla

Solution 1:

You should not be using the POST method in this instance. The POST method is meant to be used when you are changing data on the server. In this case you are only retrieving data, so GET is the more appropriate method to use here.

In any case, if you do decide to use POST, your code is actually working. You can see this if you use a POST request to access the endpoint:

$ curl -X POSThttp://127.0.0.1:5000/<div>
  [0.03464541692036849, 0.5588700799957625, 0.4702806873145451, 0.7525198710149907, 0.0674801622743858, 0.28229897849445273, 0.17400190415782735, 0.48911931330821357, 0.8033543541248421, 0.16335301905982258, 0.3307436416488905, 0.11670066397858725, 0.552907551276049, 0.6699689958218984, 0.7444295210533091, 0.8258885497774587, 0.8656500198078877, 0.6826827672886756, 0.27219907080455874, 0.9053285546116574, 0.8328655798090894, 0.2323223157770763, 0.9775485685217323, 0.34887389370958166, 0.17092511319368353, 0.20875570398480459, 0.6744092445507751, 0.6176283706166301, 0.05070680888843082, 0.3441890248079591, 0.17701427714228501, 0.115329649057473, 0.325114272097177, 0.19386610624431766, 0.18892384889766745, 0.511139418514318, 0.019931284111035397, 0.5240369332606203, 0.8936272011794374, 0.9665936114223397] 
</div>

If you just access the URL in your browser, this will actually send a GET request, and you have modified your code to specifically only allow the POST method - which is why you get the Method Not Allowed response.

$ curl -X GET http://127.0.0.1:5000/
<!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 3.2 Final//EN"><title>405 Method Not Allowed</title><h1>Method Not Allowed</h1><p>The method is not allowed for the requested URL.</p>

The best solution is for you to use GET in this instance.

Solution 2:

When you use the browser to visit a page, the browser always send 'GET' request to server... so when you've changed the methods to 'POST', flask can't find any GET route for '/' and return "Method Not Allowed" error, as the '/' doesn't allows GET anymore when browsers asks for that page.

You shouldn't use POST for this. POST only used when submitting data from forms or ajax.

Post a Comment for "Changing Get To Post In Python (flask)"