K. Ajay
K. Ajay

Reputation: 417

Getting a 400 Bad Request error code in Angular 5 POST Call

I have an ionic app through which i'm calling a rest service with a POST method. I am sending a large string of base64 data to the server. Everything works fine if i send the request through Postman. But, when i send it through the App, it gives me a 400 Bad Request error. This is my angular provider code :-

 uploadPic(bugImage : any){
    const headers1 = new HttpHeaders();
    headers1.append('Content-Type', 'application/json');

    return this.http.post(this._global.LocalserviceURL + 'ReportaBug', JSON.stringify(bugImage),{headers : headers1}).map(result => result).catch(this.errorHandler);
  }
  errorHandler(error: HttpErrorResponse) {
    return Observable.throw(error.message || "Sever Error");
  }

and this is how i am using the provider method :-

onBasicUpload(e: any) {
    this.imgpov.uploadPic(this.test).subscribe(res => {
      console.log(res);
    })

I've looked online and it says to send headers too. I guess i'm sending appropriate headers. How do i get this working?

Upvotes: 1

Views: 31319

Answers (2)

Emon
Emon

Reputation: 1549

I find out the root cause should be the Content-type haven't been updated in your request header.

Try to use const headers1 = new HttpHeaders().set('Content-Type', 'application/json'); like Angular HttpClient doesn't send header enter image description here

Upvotes: 1

RajuPedda
RajuPedda

Reputation: 3379

You need to see the 'content-type headers'

headers: new HttpHeaders( {
      **'Content-Type': 'application/json'**,
      'Authorization': 'Basic ' + token
    } )

Or

headers: new Headers({'Content-Type': 'application/x-www-form-urlencoded'

So based on Requested content-type you need to add those headers.

Upvotes: 0

Related Questions