简体   繁体   中英

Angular Post request with response access response.body issue

I'm sending a POST request to my server and I need to access the response.body.

Unfortunately, I'm not finding a method of doing that

The POST request

bday(id: number) {
let params = this.userParams;
return this.http
  .post<User[]>(environment.apiUrl + 'user/bday/' + id, {
    observe: 'response',
    params,
  })
  .pipe(
    map((response) => {
// I need to access the response.body
        })
      );
  }

But I'm getting the error: Property 'body' does not exist on type 'User[]' no matter what I'm trying.

I tried to declare the value response as HttpResponse<User[]> but that didn't work either.

I'm wondering why I'm not able to access the body of the response?

I did a Get request and everything went smooth:

Get request

getUseres(params) {
return this.http
  .get<User[]>(environment.apiUrl + 'users', {
    observe: 'response',
    params,
  })
  .pipe(
    map((response) => {
      this.paginatedResult.result = response.body;
      if (response.headers.get('Pagination') != null) {
        this.paginatedResult.pagination = JSON.parse(
          response.headers.get('Pagination')
        );
      }
      return this.paginatedResult;
    })
  );

}

Does anyone had that issue before and know how to fix it? Thanks!!!

The post method signature is post(url, body, options)

So its probably not working because your setting your options as the second argument instead of the third one.

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM