Angular HttpClient request method how to set body

16,878

Using http.get() is just a shorthand for http.request('GET'). If you really need to send a JSON body, then you'll have to use another type of request - such as post. Something like this might be what you need:

return this.http
  .post<ISubscriber[]>(
    this.resourceUrl,
    '[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
    {
      params: options
    {
  )

You may need to change your API endpoint to expect a different HTTP verb.

Share:
16,878
Bruno Miguel
Author by

Bruno Miguel

Updated on June 04, 2022

Comments

  • Bruno Miguel
    Bruno Miguel almost 2 years

    I need to send a get request with body. Im using angular HttpClient. I understand that get method does not allow to send body, so i'm triyng the request method instead but i cant understand how to use it.

    I was able to get data from the exemple bellow without the body part, but i really need to send the body as JSON format.

        request(req?: any): any{
    
        const options = createRequestOption(req);
        return this.http
            .request<ISubscriber[]>("GET", this.resourceUrl,
            {
                body: '[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
                headers: new HttpHeaders({'Content-Type' : 'application/json'}),
                params: options,
                observe: 'response'
            });
    }