1

I have this odd problem where my query parameters of get requests do not work in a prod build, but they do work in a development build.

First I was using the HttpModule, I upgraded to HttpClientModule with hopes the problem might be solved. But unfortunately without success.

When switching to HttpClient I first came across this problem where nested objects were not passed properly. I fixed this by stringifying all nested objects.

Still, the problem remains where no query paramaters are passed when using a prod build.

This is the method that I am using to stringify the data before it is passed to HttpParams:

stringifyObjects() {
  for (var prop in this) {
    let isObject = typeof this[prop] === 'object' && this[prop] !== null

    if(isObject) {
      this[prop] = JSON.stringify(this[prop]) as any;
    }
  }
}

This is the code I am using to pass the params to the request

let params = new HttpParams({
  fromObject: data
})

this.http.get(url, {params: params, headers: headers})

Also, I have tried to append every parameter separately:

let httpParams = new HttpParams();

Object.keys(data).forEach(function (key) {
  httpParams = httpParams.append(key, data[key]);
});

return this.http.get(url, {params: httpParams, headers: headers})

All with the same result.

Anyone else who came across this issue, or anyone who knows what might be causing this?

Thanks in advance.

EDIT

I added this line to check where the problem starts.

console.log('params.toString()', params.toString());

Found out the this params.toString() returns an empty string (while with a development build, this returns the string correctly). My approach with this was to manually add the string to the url instead of passing is as params.

Still, I have not found a way to fix this or work around this.

TheBurgerShot
  • 1,586
  • 2
  • 11
  • 20
  • Params and Query params are different for angular. I am unsure what the issue is and if setting is the issue. Alternatively can you check with for...in loop and see if scope resolution is the issue. It's unlikely though. I have face similar issue with append though. Can you check this this might help - https://stackoverflow.com/questions/45470575/angular-4-httpclient-query-parameters – Gary Nov 18 '18 at 06:01
  • the best tool for building `HttpClient.get()` query params is string template: `.get(\`${url}?param1=${param1}&...\`)`, using backward quotes – WildDev Nov 18 '18 at 10:43
  • @TheBurgerShot Have you ever found a solution ? I have the exact same issue. – Muj Jun 22 '20 at 13:44

1 Answers1

0

instead

let params = new HttpParams({ fromObject: data })

use

let params = { fromObject:data }

or

let params = {};
params['fromObject']=data

that work for me - angular 8.3.26

Shaybakov
  • 618
  • 7
  • 9