2

I am trying to upload an image with angular 12 and spring boot,

the api on the back side behaves correctly for having tested it with postman.

When I test front side I get this error:

"status: 415, error: "Unsupported Media Type", message: "Content type 'application/json' not supported","

******* here my ts file : *******


host = environment.API_URL;
  private baseUrl = this.host;

  constructor(private http: HttpClient) { }

  upload(file: File): Observable<HttpEvent<any>> {

    const formData: FormData = new FormData();

    formData.append('file', file);
 
    const req = new HttpRequest('POST', `${this.baseUrl}/uploadFile`, formData,{
      reportProgress: true, 
      responseType: 'json'
    });

    return this.http.request(req);
  }

  getFiles(): Observable<any> {
    return this.http.get(`${this.baseUrl}/files`);
  }

}

Thanks for your help

1 Answer 1

3

Well, the error message being returned from the server seems to be pretty clear: 'Content type application/json' not supported'.

Usually the content-type for file uploads is multipart/form-data, so you should try to set this as following:

const req = new HttpRequest(
      'POST',
      `${this.baseUrl}/uploadFile`,
      formData,
      {
        headers: new HttpHeaders({ 'content-type': 'multipart/form-data' }),
        reportProgress: true,
        responseType: 'json',
      }
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.