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.

3
  • 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 - stackoverflow.com/questions/45470575/… Commented Nov 18, 2018 at 6:01
  • the best tool for building HttpClient.get() query params is string template: .get(`${url}?param1=${param1}&...`), using backward quotes Commented Nov 18, 2018 at 10:43
  • @TheBurgerShot Have you ever found a solution ? I have the exact same issue. Commented Jun 22, 2020 at 13:44

1 Answer 1

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

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

Comments

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.