@Glen Sale Welcome to Microsoft Q&A Forum, Thank you for posting your query here!
Here is the sample code which follows this documentation:
As pre-requisites, you need to first follow the steps mentioned here.
const fs = require('fs');
const path = require('path');
// Read the JSON file
const data = fs.readFileSync(path.join(__dirname, 'pii-detection.json'), 'utf8');
const postUrl = "https://MyLanguageAiService.cognitiveservices.azure.com/language/analyze-documents/jobs?api-version=2023-11-15-preview";
const headers = {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": "84b4e3XXXXXXXXXXXXX3d6c360cc"
};
fetch(postUrl, {
method: "POST",
headers: headers,
body: data // use the data read from the file
})
.then(response => {
const operationLocation = response.headers.get('operation-location');
return operationLocation;
})
.then(operationLocation => {
const intervalId = setInterval(() => {
fetch(operationLocation, {
method: "GET",
headers: headers
})
.then(response => response.json())
.then(data => {
if (data.status !== 'running' && data.status !== 'notStarted') {
clearInterval(intervalId);
if (data.status === 'succeeded') {
data.results.documents.forEach(document => {
document.targets.forEach(target => {
console.log(target.location);
});
});
}
}
})
.catch((error) => console.error('Error:', error));
}, 1000);
})
.catch((error) => console.error('Error:', error));
The above javascript sample code :
- Reads a JSON file: It uses the fs (file system) module in Node.js to read the content of a JSON file named ‘pii-detection.json’ located in the same directory as the script. The content of the file is stored in the data variable.
- Makes a POST request: It sends a POST request to the URL specified in postUrl (which appears to be an API endpoint for Azure’s Language AI Service). The headers for this request include a content type of ‘application/json’ and a subscription key. The body of the request is the data read from the ‘pii-detection.json’ file.
- Handles the response: It then handles the response from the POST request. If the request is successful, it retrieves the ‘operation-location’ from the response headers.
- Polls the operation location: It starts an interval that sends a GET request to the operation location every second. This is likely checking the status of a long-running operation that was started by the POST request.
- Processes the operation results: Once the operation status is neither ‘running’ nor ‘notStarted’, it stops the interval. If the operation status is ‘succeeded’, it logs the ‘location’ property of each ‘target’ in each ‘document’ in the operation results.
- Error handling: If any errors occur during the fetch operations, they are logged to the console.
Note: I haven't tested the above code at my end, since I am waiting for the approval from the gating team to provide access to the Analyze Documents public preview feature.