I am hitting carrot2s api with:
const xml =
`<?xml version="1.0" encoding="UTF-8"?>
<searchresult>
<query>bob</query>
<document id="https://api.cognitive.microsoft.com/api/v7/#WebPages.0">
<title>Bob O's Family Fun Center | El Paso, TX</title>
<url>http://www.bing.com/cr?IG=29AD4AB87B7B438D8F2AA5967E17967DCID=24C5C22679EF67D1293BCE6078F66638rd=1h=AaGhCeGCFZPjz86DB6AZMIlM8b3VlbHiXWyUPsmFL_k=1r=http%3a%2f%2fbobosfun.com%2fp=DevEx.LB.1,5071.1</url>
<snippet>Bob-O’s is El Paso’s Premier Family Fun Center. Located on the Westside off Sunland Park Drive, Bob-O’s offers a variety of entertainment for the entire family.</snippet>
</document>
</searchresult>`
fetch(`carrot2server/dcs/rest`, {
method: 'POST',
body: {
'dcs.c2stream': xml,
'results': 100,
'dcs.algorithm': 'lingo',
'dcs.output.format': 'JSON'
}
})
.then(function(response) {
response.text().then(function(text) {
console.log(text);
})
}).catch(function(err) {
console.log(err)
});
If I run the same request in postman I get the expected results.
The error that I am receiving is:
HTTP Status 400 – Bad Request
Either dcs.source or a non-empty document list in dcs.c2stream must be provided
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
This was fixed by setting the correct encodeURIComponent and adding the content type: application/x-www-form-urlencoded
const params = {
'dcs.c2stream': xml,
'results': 100,
'dcs.algorithm': 'lingo',
'dcs.output.format': 'JSON'
}
const searchParams = Object.keys(params).map((key) => {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
}).join('&');
fetch('carrot2server/dcs/rest', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: searchParams
})
.then(function(response) {
response.text().then(function(text) {
console.log(text);
})
}).catch(function(err) {
console.log(err)
});