-1

I'm trying to push an entire line of text like - Hi, What's up? to a Flask REST API through curl via - curl -X POST "http://localhost:5000/predict/Hi, whats up".

The issue I have with is when the string being passed is with spaces (special characters) i.e curl -X POST "http://localhost:5000/data/Hiwhatsup" works fine but the earlier one throws a bad request. Can anyone explain why this happens? Or how to circumvent it?

The code I'm using is as follows:

@app.route("/data/<string:query>", methods=["POST"])
def data(query):
    incoming = query
    print(incoming)
randomir
  • 17,989
  • 1
  • 40
  • 55
Rahul Dev
  • 602
  • 1
  • 6
  • 16

2 Answers2

2

The problem is 'Hi, whats up' is not a valid string to be used in URLs, you should urlencode it before usage to escape symbols and get 'Hi%2C%20whats%20up'. Since now, you can make a request and handle it

P.S. your view is /data/<string:query> but should be /predict/<string:query>?

py_dude
  • 822
  • 5
  • 13
1

You could force this by URL-encoding your query with --data-urlencode, combined with -G to force appending of this encoded data as a query argument, and explicitly specifying the POST method:

curl -X POST 'http://localhost:5000/data' --data-urlencode 'Hi, whats up' -G

But you probably don't actually want to do it like that. You probably just want to POST regularly to your controlled endpoint and use something like:

curl 'http://localhost:5000/data' -d 'query=Hi, whats up'

In this case, your code has to be modified a little bit to use request.form:

@app.route("/data", methods=["POST"])
def data():
    incoming = request.form.get('query')
    print(incoming)
randomir
  • 17,989
  • 1
  • 40
  • 55