REST API call to ksql is failing with response code 403

I am using following Web Works script to call Rest API KSQL DB call. This works fine from Postman but it is failing with response code 403 forbidden error from this script on fetch(url):

onmessage = async (event) => {
const url = “https://pksqlc-**.eastus.azure.confluent.cloud:443/ksql”;

const body = {

ksql: "show streams;",

streamsProperties: {}

};

let headers = new Headers();

headers.append(“Accept”, “application/vnd.ksql.v1+json”);
headers.append(“Content-Type”, “application/json”);
headers.append(“Access-Control-Allow-Origin”, “*”);
headers.append(“Access-Control-Allow-Methods”, “GET, POST, PATCH, PUT, DELETE, OPTIONS”);
headers.append(“Access-Control-Allow-Headers”, “Origin, Content-Type, X-Auth-Token”);
headers.append(“Authorization”,“Basic *******d3k1”);

console.log(‘Worker - Before post call’);
const response = await fetch(url, {
mode: “no-cors”,
method: “POST”,
headers: headers,
body: JSON.stringify(body),
});
const reader = response.body.getReader();
function push() {
// “done” is a Boolean and value a “Uint8Array”
reader.read().then(({ done, value }) => {
if (done) {
return;
}
// get the data chunk and post a message to main thread

 const streamData = new TextDecoder().decode(value);

 postMessage(streamData);

 // try to read new message

 push();

});

}

push();

};

I appreciate any help!

Thanks
Krishna

1 Like

Update: This seems like CORS issue. How to configure CORS in Confluent Cloud KSQLDB to make Rest API call from my page?
I have following headers in my page, but still getting 403 forbidden error.

headers.append(“Access-Control-Allow-Origin”, “*”);
headers.append(“Access-Control-Allow-Methods”, “GET, POST, PATCH, PUT, DELETE, OPTIONS”);
headers.append(“Access-Control-Allow-Headers”, “Origin, Content-Type, ACCEPT, Authorization”);

I’m getting the same error whenever I try to query ksql from my browser using JavaScript. Will keep you updated if I find a way to resolve this.

This topic was automatically closed after 30 days. New replies are no longer allowed.