Search code examples
pythoncurlflask-restful

Accessing Flask REST Api with Curl doesn't work


I have built a very simple flask api which asks a neural network what kind of language has been given via POST.

Request

curl -H "Content-Type: application/json" -X POST -d "{"""text""":"""This should be recognized as an english text."""}""" http://<IP>:5000/api/v1.0/findlanguage

Api calls via GET are working within the browser, and they return all desired json objects. Flask is also responding on the serverside that there has been a GET request which he returned with HTTP Code 200.

But the POST request above doesn't provoke any output from Flask. It enters some kind of console I guess because I all see is this:

>_

That's it. What might this be? Am I missing something?

Expected output should be a json object like:

{
   'task': 'findlanguage',
   'result': 'english',
   'api': 'v1.0',
   'call': 'http://<ip>:5000/api/v1.0/findlanguage'
}

Solution

  • The request isn’t getting sent at all; the shell’s waiting for further input before running curl at all.

    But the POST request above doesn't provoke any output from Flask. It enters some kind of console I guess because I all see is this:

    >_
    

    That’s what you’ll see if the command has unbalanced quote characters. For example, try:

    curl -H "Content-Type: application/json""
    

    If you type another " at that prompt and hit the return key, the command will execute.

    That’s just an example though. The problem in the command example in the question isn’t due to the quotes around that Content-Type string but instead due to the argument to the -d option.

    Are you really using """ three-doublequotes-in-a-row there. If so, why?

    Regardless, you got a single doublequote character before the open { brace character there, and then three doublequote characters after the closing } brace, which sorta clearly seems not right.

    Why don’t you just put the -d argument in single quotes:

    -d '{"text":"This should be recognized as an english text."}'