Home > Blockchain >  Flask: make POST requests to development server
Flask: make POST requests to development server

Time:10-15

I can run a Flask app/API on my local machine using flask run on the command line. This will set up a local server (for me, at http://127.0.0.1:5000/), and run the app at that address.

Having done this, I can make GET requests to my app just by visiting http://127.0.0.1:5000/<route> in my browser. How can I make a POST request to the app? I also have parameters I want to include in the body of the POST request.

CodePudding user response:

You can't make request POST using URL in browser. It needs HTML page which has

<form method="POST">

</form>

so your server would have send this page to you.


Instead of browser you can use Python modules like urllib or simpler requests which can run .get(), .post(...), etc.

In example I use enter image description here


You may also try to use console programs like curl

curl https://httpbin.org/post -X POST -d "search=hello world"
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "search": "hello world"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "18", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.68.0", 
    "X-Amzn-Trace-Id": "Root=1-61687da3-5eaaa4ff6419c36639a2cc5d"
  }, 
  "json": null, 
  "origin": "83.11.118.179", 
  "url": "https://httpbin.org/post"
}

BTW:

Some API uses curl in documentation as example to show how to use API.

There is page https://curl.trillworks.com which can convert from curl to Python requests (but sometimes has problem to do it correctly)

  • Related