0
votes

I am trying to get the response body of a HttpClient Post request. This is kinda where I am at right now, although I have tried a dozen different variations of this based of of the hundreds of frustratingly useless examples online.

  return this._http
  .post<HttpResponse<any>>(
    apiURL,
    request,
    {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      }),
      observe: 'response' as 'body'
    }
  )
  .pipe(
    map((response: HttpResponse<any>) => {
      if (response.status === 200 || response.status === 406) {
        return response.body as RequestResponse;
      }
      throw Error("There was an error processing your request.");
    })
  );

The API, which I have no control over returns a 406 with a response body that I need to access. However I cannot for the life of me get this to enter the map there. I can catchError in the pipe and stop at a breakpoint there, but the "error" in that response is just [].

    catchError((error: HttpErrorResponse) => {
       return throwError(error); <-- error is always [];
    })

Mind you, I don't necessarily want to throwError there, I have tried other things but if error is always [], its pretty useless anyway.

The api itself is a C# azure function, and returns:

ObjectResult(theObjectINeed) { StatusCode = StatusCodes.Status406NotAcceptable }

I would basically like to just swallow the 406 and return the response body to the subscriber. I have been searching for hours on how to do this, but the docs are horrible, and most of the search results return stuff that is years old, which probably worked at one point, but no longer.

2

2 Answers

0
votes

the response body of 406 is something that must be provided by the API service in some cases according to the API development it can be empty.

One empty response body is what it appears to be in your case.

Simulate to see if the API service returns an empty body via POSTMAN or CURL

Sorry by my English, I'm from Brazil.

0
votes

The 406 Not Acceptable is an HTTP response status code indicating that the client has requested a response using Accept- headers that the server is unable to fulfill. This is typically a result of the user agent (i.e. browser) specifying an acceptable character set (via Accept-Charset), language (via Accept-Language), and so forth that should be responded with, and the server being unable to provide such a response.

Courtesy: https://airbrake.io/blog/http-errors/406-not-acceptable

You might want to check what is going inside the headers you have specified. If you have an authentication service, you might want to try calling the api without the headers, because it should be handled in the authentication service. On the fil side, this could be a back end issue also. Thorough code check would be needed.