简体   繁体   中英

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'. Unfortunately, it gives me the error post 500 (Internal Server Error). But when I set the attribute 'content-type' to 'application/json' everything is working perfectly fine. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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