[英]Trying to POST txt files using web api, getting 415 Unsupported Media Type
[英]Getting “415 Unsupported Media Type” when trying to create Hpalm defect using rest api
我正在嘗試使用 hpalm rest api 創建缺陷,但我總是得到“415 Unsupported Media Type”
這是我到目前為止所嘗試的:
var postOptions = {
jar: cookies, // <-- contains all recieved cookies from authentification
accept: 'application/json',
'Content-Type': 'application/xml',
url: almHost + '/rest/domains/' + domain + '/projects/' + project + '/defects',
body: data
};
request.post(postOptions, function (error, response, body) {
if(error) return reject(error);
else resolve(response);
});
這是我通過正文發送的數據:
var data = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
+ '<Entity Type="defect">'
+ '<Fields>'
+ '<Field Name="user-06">'
+ '<Value>MD-4493LS</Value>'
+ '</Field>'
+ '<Field Name="name">'
+ '<Value>Test with reference 2021</Value>'
+ '</Field>'
+ '<Field Name="description">'
+ '<Value>description</Value>'
+ '</Field>'
+ '<Field Name="priority">'
+ '<Value>Medium</Value>'
+ '</Field>'
+ '<Field Name="severity">'
+ '<Value>Critical</Value>'
+ '</Field>'
+ '</Fields>'
+ '</Entity>';
我不知道這段代碼有什么問題。 身份驗證工作正常,因為我能夠獲取 Alm 缺陷。
任何人都可以幫助我嗎?
你的想法很好,但我認為問題在於你用postOptions操作誰
你已經寫過:
var postOptions = {
jar: cookies, // <-- contains all recieved cookies from authentification
accept: 'application/json',
'Content-Type': 'application/xml',
url: almHost + '/rest/domains/' + domain + '/projects/' + project + '/defects',
body: data
};
但似乎 Content-Type 就在 object 中自由存在。 目前它不是一個合適的屬性。
由於 Content-Type 是 HTTP header,我們需要以某種方式指出這一點。 對於請求,有一個屬性標頭,其中包含您要添加到請求中的所有自定義標頭。
嘗試這樣做:
var postOptions = {
jar: cookies, // <-- contains all recieved cookies from authentification
accept: 'application/json',
headers: {
'Content-Type': 'application/xml',
'Content-Length': Buffer.byteLength(data)
}
url: almHost + '/rest/domains/' + domain + '/projects/' + project + '/defects',
body: data
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.