[英]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.