0

I try to send jQuery AJAX request to my flask server :

$.ajax({
            type: 'GET',
            url: '/get',
            dataType: "json",
            contentType:"application/json",
            data: JSON.stringify({ subject : "gpu",
                    filter : {
                        ids: [2, 3]
                        }
                    }),
            success: function (data) {
                console.debug(data);
            }
        });

And then I wait for a response from the server. Server part looks like this:

@api.route('/get', methods=['GET'])
def get():
    response = None
    try:
        data = request.get_json()
        response = do_some_magic(data)
    except Exception as e:
        respond = {'state': 'error', 'data': e.message}
    finally:
        return json.dumps(respond)

So, this combination doesn't work. request has only args field = ImmutableMultiDict([('{"subject":"gpu","filter":{"ids":[2,3]}}', u'')]) and json field = None.

But when in ajax request I set type: 'GET' and in flask get method methods=['GET'], server starts to handle requests correctly.

So, it would not be a real issue, but then I tried to send a GET request with postman utility. It's request:

GET /get HTTP/1.1
Host: localhost:5000
Content-Type: application/json
cache-control: no-cache
Postman-Token: 1d94d81c-7d93-4cf6-865a-b8e3e28278c1
{
    "subject": "gpu",
    "filter": {
        "ids": [
            2,
            3
        ]
    }
}------WebKitFormBoundary7MA4YWxkTrZu0gW--

And flask code worked with methods=['GET']. So the question is, what can cause such behaviour?

2
  • Possible duplicate of HTTP GET with request body Commented Feb 16, 2019 at 0:22
  • @J.J.Hakala Yeah, but the question is, why GET requests from postman are processed correctly? Commented Feb 16, 2019 at 0:29

1 Answer 1

1

From jQuery documentation

data Type: PlainObject or String or Array Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

If processData is set to false, it means that the data string is still appended to the URL, just without some processing.

This may be due to the behaviour of XMLHttpRequest.send()

send() accepts an optional parameter which lets you specify the request's body; this is primarily used for requests such as PUT. If the request method is GET or HEAD, the body parameter is ignored and the request body is set to null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.