How to pass an array within a query string in HttpClient?

前端 未结 11 878
面向向阳花
面向向阳花 2021-02-02 09:03

This is my array of actors:

[\'Elvis\', \'Jane\', \'Frances\']

How to pass this array within a query strin

11条回答
  •  余生分开走
    2021-02-02 09:21

    I'm using URLSearchParams instead of HttpParams.

    With URLSearchParams you need to stringify your array to set it to your Params "Key-Value-Store".

    import { URLSearchParams } from '@angular/http';
    
    
    let params: URLSearchParams = new URLSearchParams();
    params.set('actors', JSON.stringify(yourArrayHere));
    

    I think this should work on the same way with HttpParams because both using a Key-Value mapping in the set method so give it a try.

    I hope this can help you.

    UPDATE:

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

    with RequestOptions you can also edit Header and other request options.

提交回复
热议问题