G
G
Ghoulll2020-05-16 15:49:08
JavaScript
Ghoulll, 2020-05-16 15:49:08

Why is the http request being built incorrectly?

There is a method to send a request:

getStrongAlcoholReports(selectedItems?): Observable<ReportModel> {
    let selecteditemstest = [];
    let apiParams;
    if (selectedItems) {
      for (let code of selectedItems) {
        for (let codesarr of code.codes) {
          selecteditemstest.push(codesarr);
          apiParams = selecteditemstest.join('&codes=');
        }
      }
      return this.http.get<ReportModel>(this.listOfStrongAlcoholReports, { params: { codes: apiParams } });
    } else {
      return this.http.get<ReportModel>(this.listOfStrongAlcoholReports, { params: { codes: '200&codes=262' } });
    }
  }

But, when I looked at which request was built, it turned out to be wrong. Why is & being replaced by %26 and how to fix it? Thank you all in advance.
5ebfe0ecd55e5848283217.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-05-16
@Ghoulll

Well, something like this

const params = codes.reduce(
  (acc, code) => acc.append('codes', `${code}`),
  new HttpParams(),
);
this.http.get<ReportModel>(this.listOfStrongAlcoholReports, { params });

where codesit is [200, 262 ]
Do not specify params as an object, there is a class for HttpParams
this
const params = new HttpParams({
  fromObject: {  codes:  codes.map(String)  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question