1

I am taking in bunch of inputs from the user in html which I am then passing on to ajax query to get the response.

$.ajax({
  url:"http://0.0.0.0:8080/getReport",
  type:"GET",
  data:JSON.stringify(out),
  dataType:"json",
  contentType:"application/json"
})

Here is the Flask code that serves the above request.

@app.route('/getReport', methods=['GET'])
def report():
    return Response('this is a sample response')

The above method is not able to find the route to the 'report' with get. However, it is able to find it in POST request.

This is the log that I am getting

  127.0.0.1 - - [25/Apr/2016 13:00:03] "GET /getReport?{%22insertion_id%22:%22%22,%22start%22:%22%22,%22end%22:%22%22} HTTP/1.1" 400 -

Bad Request.. What am I doing wrong here?

3
  • you are using JSON as URL parameters, why? Commented Apr 25, 2016 at 18:36
  • I don't think Flask accepts json data from client. You should send data as urlencoded, like data: {'key': 'val',}. No need to set dataType and contentType options. Commented Apr 25, 2016 at 19:01
  • You should serialize form data and not specify the dataType or contentType. Send across and it should work fine. Commented Apr 25, 2016 at 19:05

1 Answer 1

4

A GET request does not have a contentType (*) and it isn't JSON-encoded, but URL-encoded (plain, regular key-value pairs).

This means you can simply go with jQuery's default.

$.get("http://0.0.0.0:8080/getReport", out).done(function (data) {
    // request finished
});

which will result in a request like:

GET /getReport?insertion_id=&start=&end= HTTP/1.1

This will be easily understood by the server.


(*) That's because the Content-Type header determines the type of the request body. GET requests do not have a request body.

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

1 Comment

Thanks!! It helped me in my understanding!

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.