[英]How to fix the 415 / Unsupported media type error?
我正在使用角度来发布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)
我希望这个请求可以工作,因为它在邮递员工作,我用过,我认为是所有必要的标题。
HTTP post调用不正确,第二个参数是请求正文: https : //angular.io/api/common/http/HttpClient#post 。
改变这个:
this.http.post(idpUrl, idpOptions)
对此(假设您不需要任何请求正文):
this.http.post(idpUrl, null, idpOptions)
您还可以查看Angular文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.