1

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?

K. Ajay
  • 357
  • 1
  • 4
  • 21

2 Answers2

1

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

Emon
  • 1,401
  • 14
  • 24
  • I tried it, now the Content-Type has changed to application/json but i'm still getting the same error Screenshot - http://prntscr.com/o2v9nf – K. Ajay Jun 17 '19 at 11:08
  • can you give me a screenshot about the error response to check if there is any information?. – Emon Jun 18 '19 at 07:14
  • Or if it's body data issue instead of header issue? Because I saw there was no "data:image/png;base64" before your postman request – Emon Jun 18 '19 at 07:50
0

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.

RajuPedda
  • 3,141
  • 3
  • 14
  • 27