简体   繁体   English

在Angular 5 POST调用中获取400 Bad Request错误代码

[英]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. 我有一个离子应用程序,通过它我用POST方法调用休息服务。 I am sending a large string of base64 data to the server. 我正在向服务器发送一大串base64数据。 Everything works fine if i send the request through Postman. 如果我通过Postman发送请求,一切正常。 But, when i send it through the App, it gives me a 400 Bad Request error. 但是,当我通过应用程序发送它时,它给了我400 Bad Request错误。 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? 我如何让这个工作?

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. 因此,根据请求的内容类型,您需要添加这些标头。

I find out the root cause should be the Content-type haven't been updated in your request header. 我发现根本原因应该是您的请求标头中尚未更新的Content-type。

Try to use const headers1 = new HttpHeaders().set('Content-Type', 'application/json'); 尝试使用const headers1 = new HttpHeaders()。set('Content-Type','application / json'); like Angular HttpClient doesn't send header Angular HttpClient不发送头 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM