0

I am using Flask-Restless to create my API, which requires queries parameters to be formatted using a list of filter objects. A valid query follows this format:

/api/person?q={"filters":[{"name":"firstName","op":"like","val":"Mike"}]}

(See Flask-Restless query docs here.)

When I use Angular's $http.get to pass the query params, they are encoded in the URL and break the query:

GET /api/person?q=%7B%22filters%22:%7B%22name%22:%22firstName%22,%22op%22:%22like%22,%22val%22:%22Mike%22%7D%7D HTTP/1.1"

Is it possible to disable encoding for all or some of the parameters?

fejese
  • 4,601
  • 4
  • 29
  • 36
Josh
  • 662
  • 11
  • 27

1 Answers1

2

UPDATED ANSWER:

Angular will by default stringify a params value if it is an object.
Thus, it will make all necessary work for you, just by passing the queryObject:

var queryObject = {filters: [...]};
$http.get('...', {params: {q: queryObject}});

What you see is the stringified object encoded as a URI component.

That is probably not done by Angular, but by your browser itself.
(E.g. try making a simple XHR to the same URL and insect the request in the Network panel of the DevTools.)

I am not familiar with Flask, but (if it doesn't automatically decode the query param) it should have a method to do so manually.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • Even after stringifying, the server shows the request as GET /api/person?q=%7B%22filters%22:%7B%22name%22:%22firstName%22,%22op%22:%22like%22,%22val%22:%22Mike%22%7D%7D HTTP/1.1" – Josh Sep 21 '14 at 14:05
  • @Josh: Actually, I was wrong, you don't need to stringify yourself. It's browser encoding the query param as a URI component. It happens with every request and Flusk should be able to handle it. – gkalpak Sep 21 '14 at 14:57
  • Thanks @ExpertSystem, you are right. Looks like I just made the dumb mistake of not wrapping my search string in %% when using the LIKE operator. – Josh Sep 21 '14 at 15:09