将请求正文作为Node.js中的XML字符串发布

问题描述 投票:-1回答:1

我正在尝试使用提取API将XML字符串而不是JSON对象发布到node.js服务器。

这是我的发布JSON对象的代码:

handleSubmit = async e => {

    e.preventDefault();
    var request = JSON.stringify({
        drug: this.state.drug,
        disease: this.state.disease,
        type: this.state.type    
    });
    var xmlRequest = js2xmlparser.parse("request", request);

    const response = await fetch('/api/submit', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: request
    });

    const body = await response.text();

    this.setState({
        responseToPost: body
    });
}

如何编辑代码以在请求正文中发布XML字符串(xmlRequest)而不是JSON(请求)。

node.js xml fetch fetch-api
1个回答
1
投票

发送xmlRequest而不是正文中的request。同样将Content-Type更改为text/xmlapplication/xml

const request = {
    drug: this.state.drug,
    disease: this.state.disease,
    type: this.state.type    
};

const xmlRequest = js2xmlparser.parse('request', request);

const response = await fetch('/api/submit', {
    method: 'POST',
    headers: {
        'Content-Type': 'text/xml'
    },
    body: xmlRequest
});

[js2xmlparser接受一个对象作为第二个参数,请勿使用JSON.stringify

© www.soinside.com 2019 - 2024. All rights reserved.