I am not new to programming, but I have some issues with some API requests.
I have the next route (example):
The return in postman is OK (example):
And my code is (example):
let url = 'https://test.com/api/products';
let username = '1234567890abcdefg';
let password = '';
let headers = new Headers();
headers.set('Authorization', 'Basic ' + base64.encode(username + ":" + password));
async function testAPI(){
const res = fetch(url, {
method: 'GET',
headers: headers,
}).then(function (resp){
return resp.text();
}).then(function(data){
let parser = new DOMParser(),
xmlDoc = parser.parseFromString(data, 'text/xml')
console.log(xmlDoc)
return (xmlDoc)
})
}
With my code, I have the next issue: EventSource's response has a MIME type ("application/json") that is not "text/event-stream". Aborting the connection.
As I understand, I have some lack of knowledge about API request returns XML file.
What I trying to do:
I am trying to get data from API request, but API request returns data in XML format. I like to transform XML to JSON after and make next actions.
P.S. How I do this with JSON format (example). I like to do this exactly, but with XML.
async function testAPI(){
const res = await fetch('https://test.com/api/products')
const json = await res.json()
return { apiRawData: json }
}