0

I am trying to encode ("application/x-www-form-urlencoded") an object in Angular using HttpParameterCodec. This object has a field as object which in turn has array of string. object structure as mentioned below.

export class SearchRequest {id: string = undefined;partnerIds: PartnerIds = undefined;}


export class PartnerIds { partnerId: string[] = undefined;}

Now when I am trying encode this object with utility code (which works fine with plain object) mentioned below, It is not in correct format due to list of string in it.

generateQueryParams(searchRequest) {
    let httpParams: HttpParams = new HttpParams({encoder: new CustomEncoderComponent()});
    Object.keys(searchRequest).forEach(key => {
      httpParams = httpParams.append(key, searchRequest[key]);
    });

    return httpParams;
}

I tried with below query params format but it did not work.

1.   id=123&partnerIds=XYZ&partnerIds=ABC
2.   id=123&partnerId[]=XYZ&partnerId[]=ABC
3.   id=123&partnerId=XYZ&partnerId=ABC

Please suggest How to pass this (SearchRequest) object within a query string in HttpClient?

Dhiraj333
  • 11
  • 3
  • There's no standard for how to encode anything other than plain key-value pairs to URL encoding. Various languages or frameworks have their own way. `foo=bar&foo=baz`, `foo[]=bar&foo[]=baz`, `foo.0=bar&foo.1=baz`… It's basically up to you and how your server can/will deal with it. – deceze Sep 08 '20 at 09:41
  • Thanks deceze for your reply. Problem here with any of these is I am getting null values for partnerIds field at Java Rest endpoint. – Dhiraj333 Sep 08 '20 at 11:18

0 Answers0