Reputation: 55
I am trying to post XML string instead of JSON object to node.js
server using fetch API.
This is my code which post JSON object:
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
});
}
How to edit the code to post XML string(xmlRequest) instead of JSON (request) in the request body.
Upvotes: 0
Views: 6464
Reputation: 40434
Send xmlRequest
instead of request
in the body. Also change the Content-Type
to text/xml
or application/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
takes an object as second argument, don't use JSON.stringify
.
Upvotes: 2