Repository navigation
Expand file tree
/
Copy pathbasic.js
More file actions
60 lines (52 loc) · 1.69 KB
/
Copy pathbasic.js
File metadata and controls
60 lines (52 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* XML to JSON API - Basic Usage Example
*
* This example demonstrates the basic usage of the XML to JSON API.
* API Documentation: https://docs.apiverve.com/ref/xmltojson
*/
const API_KEY = process.env.APIVERVE_API_KEY || 'YOUR_API_KEY_HERE';
const API_URL = 'https://api.apiverve.com/v1/xmltojson';
/**
* Make a POST request to the XML to JSON API
*/
async function callXMLtoJSONAPI() {
try {
// Request body
const requestBody = {
"xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<note>\n <to>Tove</to>\n <from>Jani</from>\n <heading>Reminder</heading>\n <body>Don't forget me this weekend!</body>\n</note>"
};
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'x-api-key': API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
// Check if response is successful
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// Check API response status
if (data.status === 'ok') {
console.log('✓ Success!');
console.log('Response data:', data.data);
return data.data;
} else {
console.error('✗ API Error:', data.error || 'Unknown error');
return null;
}
} catch (error) {
console.error('✗ Request failed:', error.message);
return null;
}
}
// Run the example
callXMLtoJSONAPI()
.then(result => {
if (result) {
console.log('\n📊 Final Result:');
console.log(JSON.stringify(result, null, 2));
}
});