3

What is the equivalent of $httpParamSerializer(params) from angular 1 in angular 2?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Kld
  • 6,970
  • 3
  • 37
  • 50

1 Answers1

6

I don't know about an exact equivalent, but with URLSearchParams it will handle the encoding for you. Sucks it doesn't allow you to just pass an object, so you need to do something like

import { URLSearchParams } from '@angular/http';

let params = new URLSearchParams();
for (let key in someObj) {
  if (somObj.hasOwnProperty(key)) {
    params.set(key, someObj[key])
  }
}

If all you need is the string for some odd reason, just call params.toString(). Otherwise if you want to pass it to the Http request, just do

let options = new RequestOptions({ search: params });
http.get(url, options);

The query string will get appended to the URL in a GET request, and in a POST request, you can set it as the body

http.post(url, params);
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720