Angular: HTTP Response with code 200 gets treated as error

Viewed 375

I have a simple Rest Controller in my Spring backend and an Angular Frontend which tries to access that ressource.

This is my Spring Controller:

@RestController
public class Controller {

    @PostMapping(value="/uploadFile", produces="text/plain")
    public ResponseEntity<String> uploadFile() {

        return new ResponseEntity("File got uploaded successfully", HttpStatus.OK);
    }

}

And this is my call in the frontend:

http.service.ts:

export class HttpService {

  constructor(private http: HttpClient) { }

  sendFileToBackend(): Observable<string> {  
    const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    return this.http.post<any>('http://localhost:8080/uploadFile', {
      headers, 
      responseType: 'text'
    });
  }
}

This is my Chrome console after firing the request from my frontend:

enter image description here

As you see, the error.message is the result I expect normally, just without the error itself.

What am I doing wrong here?

0 Answers
Related