Home > OS >  Wrong piped type got from HttpClient Post request
Wrong piped type got from HttpClient Post request

Time:09-18

My service is firing post request like this

createDocument(owner: string): Observable<HttpResponse<Document>> {
    return this.http.post<HttpResponse<Document>>(`${this.API}`, {owner}).pipe(
      tap(document => {
        this.dataStore.unshift(document.body!.authenticationMethod, document.body!.assertionMethod)
        this.list$.next(this.dataStore)
      })
    )
  }

However the response in the tap function doesn't have a HttpResponse type instead it is a Document.

I was expecting document to be a HttpResponse<Document> object. My APIs is responding with a Document json-like string that is correctly parsed into a Document object.

CodePudding user response:

Angular HttpClient methods support an option called observe.

By default it's body. In your case you should specify type 'response':

return this.http.post<Document>(`${this.API}`, {owner}, { observe: 'response' })

Note: I also change type to just Document so that TypeScript will automatically recognize document parameter as HttpResponse<Document>

  • Related