I have a piece of code similar to the one below that is functioning properly:
const url = "https://something";
let data2 = JSON.stringify({ source: "https://someimage.jpg" });
const test1 = fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": "mykey",
},
body: data2,
}).then((res) =>
console.log("postHeaderResult" + res.headers.get("Operation-Location"))
);
My goal now is to use the Operation-Location
header, which contains a URL address, to make a GET
request and retrieve the final response from the body of this new request.
I have attempted the following code, but unfortunately, it is not working:
const url = "https://something";
let data2 = JSON.stringify({ source: "https://someimage.jpg" });
const test1 = fetch(url_to_formRecognizer, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": "mykey",
},
body: data2,
})
.then((res) =>
console.log("postHeaderResult" + res.headers.get("Operation-Location"))
)
.then((res1) =>
fetch(test1.headers.get("Operation-Location"), {
method: "GET",
headers: {
"Ocp-Apim-Subscription-Key": "34dd609bcb5742de91e8474f0e27e88e",
},
}).then((res1) => console.log("finalResult" + res1 + "simple"))
);
When testing my code here, I encounter an error related to test1
. However, in my Visual Studio
, there are no compilation errors, even though the GET request is not executed.
I have even tried to capture the error using the following line:
.catch((err) => console.log("odsPolicyRequestError" + err));
but it remains empty. I am unable to identify the issue and where I may have made an error.
Your assistance in resolving this matter would be greatly appreciated.
Update 1:
Here is the updated code based on the suggestion. However, I am still receiving "not started yet" as the output of my second call. It appears that the second call is initiated before the first one is completed.
const url = "https://something";
let data2 = JSON.stringify({ source: documentUrl });
const res1 = await fetch(url, {
method: "POST",
headers: { 'Content-Type': 'application/json' , 'Ocp-Apim-Subscription-Key': apiKey},
body: data2
});
if (!res1.ok) {
console.log("postHeaderResult not okay first fetch" + res1.status + " on first fetch");
}
console.log("header is:" + res1.headers.get("Operation-Location"), null);
const res2 = await fetch(res1.headers.get("Operation-Location"), {
method: "GET",
headers: {'Ocp-Apim-Subscription-Key' : apiKey }
}).then( res2 => { return res2.text(); }).then(text => { api.output("text" + text, null); } );