0

It's a small web demo. My server side is written by python-3(flask), both front-end and back-end work fine. But now I want to write another client python code and try to send data(say, 'foobar') to my server.

Here's part of my server side code.

@app.route('\', methods=['POST', 'GET']
def show_index():
    return render_template('index.html')

I am not sure how to write the client code and how to modify my server code.

TianpingHsu
  • 91
  • 2
  • 5
  • [The documentation](http://flask.pocoo.org/docs/1.0/patterns/fileuploads/) contains all the information you need to be able to upload files to the server. – Tomáš Linhart Apr 20 '19 at 04:44

1 Answers1

0

Here is a minimal server and client using the requests package for the HTTP client.

server.py

The server simply pulls the value of data from the Flask request objects form attribute which is populated by data from the POST request, and returns it as an HTTP 200 response.

from flask import Flask, request

app = Flask(__name__)


@app.route("/", methods=["GET", "POST"])
def show_index():
    data = request.form.get("data")
    return data, 200


if __name__ == "__main__":
    app.run(debug=True)

client.py

The client makes a POST request to the Flask server sending the data in application/x-www-form-urlencoded format.

import requests

response = requests.post("http://127.0.0.1:5000/", data={"data": "foobar"})
print(response.status_code)
print(response.text)

Steve McCartney
  • 191
  • 1
  • 3