【问题标题】:Getting a 400 Bad Request error code in Angular 5 POST Call在 Angular 5 POST 调用中获取 400 Bad Request 错误代码
【发布时间】:2019-06-17 17:58:44
【问题描述】:

我有一个离子应用程序,我通过它使用 POST 方法调用休息服务。我正在向服务器发送一大串 base64 数据。如果我通过邮递员发送请求,一切正常。但是,当我通过应用程序发送它时,它给了我一个 400 Bad Request 错误。 这是我的角度提供程序代码:-

 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");
  }

这就是我使用提供者方法的方式:-

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

我在网上查看过,它说也要发送标头。我想我正在发送适当的标题。我如何让这个工作?

【问题讨论】:

  • 400 表示你的 API url 不正确
  • 我交叉检查了网址,它很好。和我在 Postman 中使用的一样
  • 您可以检查您的应用请求和邮递员请求之间的差异。所以你可以找出哪个值不正确。
  • Postman 自动生成了 9 个临时标头。我猜这是唯一的区别。
  • 您能否检查响应消息以确保您是否缺少标头中的某些值?

标签: angular ionic-framework post postman


【解决方案1】:

我发现根本原因应该是您的请求标头中的 Content-type 尚未更新。

尝试使用 const headers1 = new HttpHeaders().set('Content-Type', 'application/json'); 喜欢Angular HttpClient doesn't send header

【讨论】:

  • 我试过了,现在 Content-Type 已更改为 application/json 但我仍然收到相同的错误截图 - prntscr.com/o2v9nf
  • 能否给我截图错误响应以检查是否有任何信息?
  • 或者如果是正文数据问题而不是标题问题?因为我看到在你的邮递员请求之前没有“data:image/png;base64”
【解决方案2】:

您需要查看“内容类型标头”

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

或者

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

因此,您需要根据请求的内容类型添加这些标头。

【讨论】:

  • 我已经在我的请求中添加了 'Content-Type': 'application/json' 标头。
  • 为了确保在 URL 上,您能否使用 Postman 属性检查您的查询参数(类型检查)属性?
  • 都是空白的。
  • 如果您可以分享邮递员请求和结果以及应用程序请求和响应的屏幕截图,将有助于我们进一步了解问题。
  • 邮递员标题 - prnt.sc/o2tgfo 邮递员正文 - prnt.sc/o2tgn1 应用请求 1 - prnt.sc/o2tzsp 应用请求 2 - prnt.sc/o2u06x
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
相关资源
最近更新 更多