![](/img/trans.png)
[英]set both the HTTP Accept and Content-Type headers to “application/xml” in C#
[英]Angular 2 headers with 'content-type' : 'text/xml', http post not working
抱歉,我真的不知道如何问这个问题,基本上,我想使用自定义标头调用 http.post,并将属性“content-type”设置为“text/xml”。 不幸的是,它给了我 500 后的错误(内部服务器错误)。 但是当我将属性 'content-type' 设置为 'application/json' 时,一切正常。 这是我的代码:
getXMLHeaders(): Headers {
const headers = new Headers({
'Authorization': `Basic ${localStorage.getItem('APIkey')}`,
'content-type': 'text'
});
return headers;
}
getOptionsXML(): RequestOptions {
const options = new RequestOptions({headers: this.getXMLHeaders()});
return options;
}
writeXML(file: string, xml: any, http: Http): boolean {
try {
const options = this.getOptionsXML();
const body = {
'path': file,
'body': xml
};
console.log(body);
const readFile = http.post(LOCAL_API_ADDRESS
+ localStorage.getItem('APIport')
+ '/local-api/write-File/', body, options)
.toPromise();
readFile.then(res => {
})
.catch(err => {
this.loggerServer.error(http, 'writeXML() promise error: '
+ err.message);
});
return true;
} catch (err) {
this.loggerServer.error(http, 'FileManager writeXML: ' + err.message);
return false;
}
}
在内容类型中,您通知端点您发送的数据类型。 如果您说它是 XML(文本/xml、应用程序/xml)并且服务器不接受此格式,则结果为 500。显然您的端点仅接受 JSON(应用程序/json)。
简单地说,您正在尝试以这种格式发送消息:
<content>
<param Name="ParamA">TextA</text>
</content>
而服务器只接受这种格式:
{
"ParamA":"TextA"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.