Angular 2 http post params and body

前端 未结 5 2036
暖寄归人
暖寄归人 2021-02-02 16:32

I\'m trying to do an api call from my angular app. What I want to do is send a post request to the api with a command param. I have done a lot of server side testing as well as

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 16:37

    Let said our backend looks like this:

    public async Task Post([FromBody] IList roles, string notes) {
    }
    
    

    We have a HttpService like this:

        post(url: string, body: any, headers?: HttpHeaders, params?: HttpParams): Observable {
            return this.http.post(url, body, { headers: headers, params});
        }
    

    Following is how we can pass the body and the notes as parameter: // how to call it

    const headers: HttpHeaders = new HttpHeaders({
        'Authorization': `Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXX`
    });
    
    const bodyData = this.getBodyData(); // get whatever we want to send as body
    
    let params: HttpParams = new HttpParams();
    params = params.set('notes', 'Some notes to send');
    
    this.httpService.post(url, bodyData, headers, params);
    
    

    It worked for me (using angular 7^), I hope is useful for somebody.

提交回复
热议问题