如何修复415 /不支持的媒体类型错误?

时间:2019-06-14 14:08:45

标签: angular http post

我正在使用angular进行POST。我已经在Postman中成功完成了此操作,所以现在我只是想在我的角度应用程序中获取它。

当我尝试按角度发出请求时,得到一个

Status Code: 415 / Unsupported Media Type

控制台中还有很长的错误代码

ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{"_t":"Map","_i":{},"_s":0},"lazyUpdate":null},"status":415,"statusText":"Unsupported Media Type"

因此在Postman中,我能够成功进行POST。为了使所有标题正确,我使用了Postman内置代码选项

"headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent": "PostmanRuntime/7.13.0",
    "Accept": "*/*",
    "Cache-Control": "no-cache",
    "Postman-Token": "4eac9c16-3e20-44d0-be8d-414ead01bdc6,107a109d-62dd-4b52-b84d-e60d3ad092c4",
    "cookie": "JSESSIONID=F09A8E826390A6B788B9E45E0E663ECD.pc4bsfapi06t",
    "accept-encoding": "gzip, deflate",
    "content-length": "2337",
    "Connection": "keep-alive",
    "cache-control": "no-cache"

我使用了以下标题:

"Content-Type": "application/x-www-form-urlencoded",
    "Accept": "*/*",
    "Cache-Control": "no-cache",
    "accept-encoding": "gzip, deflate",
    "content-length": "2337",
    "Connection": "keep-alive",
    "cache-control": "no-cache"

我通过执行以下操作来设置它们:

let idpOptions ={
           headers: new HttpHeaders({
              "Content-Type": "application/x-www-form-urlencoded",
              "Accept": "*/*",
              "Cache-Control": "no-cache",
              "accept-encoding": "gzip, deflate",
              "content-length": "2337",
              "Connection": "keep-alive",
              "cache-control": "no-cache"
              }),
          withCredentials: true,
        };

然后做了:

 this.http.post(idpUrl, idpOptions)

我希望这个请求能够正常工作,因为它是在邮递员中工作的,我使用了我认为是所有必要的标头。

1 个答案:

答案 0 :(得分:0)

HTTP post调用不正确,您的第二个参数用于请求正文:https://angular.io/api/common/http/HttpClient#post

更改此:

this.http.post(idpUrl, idpOptions)

对此(假设您不需要任何请求正文):

this.http.post(idpUrl, null, idpOptions)

您还可以查看Angular documentation