简体   繁体   English

带有“内容类型”的 Angular 2 标头:“text/xml”,http 帖子不起作用

[英]Angular 2 headers with 'content-type' : 'text/xml', http post not working

sorry I don't really know how to ask this question, basically, I want to make a call to http.post with a custom headers with the attribute 'content-type' set to 'text/xml'.抱歉,我真的不知道如何问这个问题,基本上,我想使用自定义标头调用 http.post,并将属性“content-type”设置为“text/xml”。 Unfortunately, it gives me the error post 500 (Internal Server Error).不幸的是,它给了我 500 后的错误(内部服务器错误)。 But when I set the attribute 'content-type' to 'application/json' everything is working perfectly fine.但是当我将属性 'content-type' 设置为 'application/json' 时,一切正常。 Here is my code :这是我的代码:

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;
    }
}

In the content-type you are informing endpoint what kind of data you are sending.内容类型中,您通知端点您发送的数据类型。 If you say that it is XML (text/xml, application/xml) and the server is not accepting this format it results in 500. Apparently your endpoint is accepting only JSONs (application/json).如果您说它是 XML(文本/xml、应用程序/xml)并且服务器不接受此格式,则结果为 500。显然您的端点仅接受 JSON(应用程序/json)。

Simply speaking you are trying to send message in this format:简单地说,您正在尝试以这种格式发送消息:

<content>
  <param Name="ParamA">TextA</text>
</content>

while the server only accepts this format:而服务器只接受这种格式:

{
    "ParamA":"TextA"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM