4

I want to expect a request where the request.json looks like:

{
  "app_name": "app",
  "model_name": "model"
}

I created the following parser:

parser = reqparse.RequestParser()
parser.add_argument('app_name', location='json', required=True)
parser.add_argument('model_name',  location='json', required=True)

And am using the parser as:

class ModelList(Resource):
    @api.expect(parser)
    def get(self):
    """Get all matching model records"""
    ....

This shows up in the service as:

enter image description here

But when I try this out, my request is translated as following:

enter image description here

I expect the request to look like:

curl -X GET "http://localhost:5000/model" -H  "accept: application/json" -H  "Content-Type: application/json" -d '{"app_name": "test","model_name": "affinity"}'

and not:

curl -X GET "http://localhost:5000/model" -H  "accept: application/json" -H  "Content-Type: application/json" -d "affinity"

What am I doing wrong?

nish
  • 6,230
  • 17
  • 60
  • 113
  • Well, a GET request can’t have a request body, no. Only a URL is sent (and headers), so you need to put your request data into the URL (as query parameters, usually) or in headers (highly unusual, almost never a good idea). Or use POST instead of GET, but that has specific connotations in a REST API. Any reason why these parameters must be JSON? – Martijn Pieters Dec 03 '19 at 22:47
  • @MartijnPieters, you can send a request body even with GET request, although it is not a common practice to do so. – MikeL Dec 22 '19 at 06:41
  • @MikeL: yes, technically it is legal for *any* request message to include a body, including GET, but no compliant server is going to do anything with it. Also see [HTTP GET with request body](//stackoverflow.com/q/978061) – Martijn Pieters Dec 22 '19 at 18:00

1 Answers1

1

TypeError: HEAD or GET Request cannot have a body.

Refer to this SO question for why it cannot (should not) have one: HTTP GET with request body

To fix, either remove location='json' or specify location='args' instead.

parser = reqparse.RequestParser()
parser.add_argument('app_name', required=True)
parser.add_argument('model_name', required=True)
parser = reqparse.RequestParser()
parser.add_argument('app_name', location='args', required=True)
parser.add_argument('model_name', location='args', required=True)

Both will let Swagger know to send the arguments in query string and the parser know to look there.

aaron
  • 17,311
  • 4
  • 27
  • 63