【问题标题】:415 Error: Postman passes, Front end does not415 错误:邮递员通过,前端不通过
【发布时间】:2020-01-15 13:16:09
【问题描述】:

我已经使用 order/:orderId 定义了一条路线,并且我正在尝试对 orderId 进行哈希处理以避免显示真实的 orderId。我研究了散列的方式,并决定使用 32 位混合函数。我已经使用 PostMan 测试了该功能,一切正常,但是在访问所需的端点时,我收到 415 错误。实现如下所示:

this.orderService.createOrder(newOrder)
      .pipe(
        takeUntil(this.ngUnsubscribe)
      ).subscribe(result => {
        console.log(result);
        this.router.navigate(['order', result])
      });

服务看起来像这样:

 createOrder(order:OrderModel): Observable<any> {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/*'
      })
    };
    return this.http.post(this.BASE_URL+'Orders/', order, httpOptions)
    .pipe(
      catchError(this.handleError('createOrder'))
    )

subscribe 中的结果应该是 orderId 的哈希版本。

【问题讨论】:

  • 显然原因是'Content-Type': 'application/*' ...没有服务器端代码,我们无法猜测预期的类型...但我 90% 确定它应该是 json (application/json)跨度>
  • 通常我会查看正确的请求(由 Postman 发送)、不正确的请求(由 Web 应用程序发送)并进行比较。有什么区别?
  • @Selvin 谢谢,那是我的问题。错过了

标签: c# angular observable postman


【解决方案1】:

415 状态码表示不支持的媒体类型,我认为将 content-type 发送为 application/json 将解决问题。

Content-type: application/json;

您的请求应如下所示

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  })
};

有时即使您在请求中发送了 application/json,它也不起作用。在这种情况下,您需要检查 IIS 并添加新的 MIME(如果不存在)。

【讨论】:

    猜你喜欢
    • 2019-10-26
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多