Timothy
Timothy

Reputation: 115

Why is it that angular/typescript http GET method does not accept JSON object as parameter

CODE SAMPLE 1

passJSONObj(){
  let name = 'user names',
  let email = '[email protected]'

  return this._http.get('url?name=usernames&[email protected]');

}

CODE SAMPLE 2

passJSONObj(){
let param = {
    name: 'usernames',
    email: '[email protected]'
}
return this._http.get('url', param);

}

Code sample 1 works fine but code sample 2 does not. I want to know the reason. I've made research but I haven't understand why such a simple code won't work

Upvotes: 1

Views: 279

Answers (1)

xrobert35
xrobert35

Reputation: 2556

The second parameter is not only for "query params". So you can try something like this

return this._http.get('url', { params : param});

See the type documentation :

 /**
     * Construct a GET request which interprets the body as text and returns the full response.
     *
     * @return an `Observable` of the `HttpResponse` for the request, with a body type of `string`.
     */
    get(url: string, options: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        observe: 'response';
        params?: HttpParams | {
            [param: string]: string | string[];
        };
        reportProgress?: boolean;
        responseType: 'text';
        withCredentials?: boolean;
    }): Observable<HttpResponse<string>>;

Upvotes: 2

Related Questions