0

I want to use jQuery's ajax call with type 'GET' against my RESTful API running on Flask. In my request I want to post some data as JSON.

I'm doing the exact same thing for POST requests and they work like charm. But with GET Flask gives me 400 errors and looking deeper into it it seems that the JSON gets partly URIEncoded on the way ({%22email%22:%22some@email.com%22}).

I've tried to use decodeURIComponent as I'm JSON.stringifying the json in the ajax data parameter but makes no difference.

sessiontoken = "123abc";
jsonData = {"email": email};

$.ajax({
    type: 'GET',
    crossDomain: true,
    url: 'http://someserver/sessions/' + sessiontoken,
    dataType: 'json',
    processData: false, //added this to see if it helps, it didn't 
    contentType: "application/json",
    async: false,
    data: JSON.stringify(jsonData),
    success: function(data){
        //I'd be happy
    },
    error: function(data){
        //This is where I get as my backend throws a 400 on me due to the screwed up json
    }
});

It's driving me nuts as I can't seem to find anyone on the planet who's had the same issue. I've been ajaxing requests before and never faced this silly thing in the past.

Edit: Ok it seems like I need to give up on my goal and just pass whatever parameters as query string instead of trying to add them to the request body. I guess there is nothing wrong with that as discussed for example here: REST API Best practices: Where to put parameters?

Community
  • 1
  • 1
Timo Wallenius
  • 447
  • 5
  • 15

2 Answers2

2

I suspect you want to append jsonData as query parameters. For doing so you don't need to encode it.

$.ajax({
  ...
  data: jsonData,
  ...
 });

Your resulting URL will be like

http://someserver/sessions/123abc?email=foo%40bar.com
Alexander
  • 22,498
  • 10
  • 56
  • 73
  • Exactly. With my first attempt at a RESTful API I've understood that the resource should be identified the sessiontoken. And I will check the validity of the token by comparing the token with an email. And the place for the email should not be in the uri but through for example JSON. – Timo Wallenius Aug 26 '12 at 07:07
1

If you trying to set any data to GET request of ajax, it will be converted to parameters of URL string. Ofcourse any symbols like " will be represented as %URL_CODE (see all codes), and you will get query like next:

http://someserver/sessions/sessiontoken?{%22email%22:%22some@email.com%22}

Best way to organize your restful service is descript queries in URI parts. Another way you could put json into URL parameters, and parse url codes for symbols on server-side.

Arkady
  • 1,062
  • 10
  • 31
  • This way lacks URL cleanness. The alphabet for email addresses will make your URL a mess, hence less RESTful. And, parsing URL codes has nothing to do here, this is usually transparent in most frameworks and Flask should not be the exception – Alexander Aug 25 '12 at 21:00
  • That's right, just haven't thought about it. I've edited an answer. – Arkady Aug 25 '12 at 21:26
  • @Alexander, one of goals of REST is cut key value pairs in query parameters, that's why I'm thinking that it's possible to include JSON there. Or I'm wrong? At least I've seen it somewhere. – Arkady Aug 25 '12 at 21:37
  • Is this really the way to go? I thought I could bundle everything that doesn't indicate the actual resource in question (like email in this case) somewhere else and URI. I'm sure GET with parameters is done million times and I'm surprised if this is the best practice. With post I can just read request.json["email"] on my backend. With this approach it seems like some hack reading and parsing from the uri in Flask. – Timo Wallenius Aug 26 '12 at 07:14
  • Ok it seems like I need to give up on my goal and just pass whatever parameters as query string instead of trying to add them to the request body. I guess there is nothing wrong with that as discussed for example here: REST API Best practices: Where to put parameters? – Timo Wallenius Aug 27 '12 at 12:18