Help

External POST in Extensions using Block

Topic Labels: Custom Extensions
1015 0
cancel
Showing results for 
Search instead for 
Did you mean: 
RamunasP
4 - Data Explorer
4 - Data Explorer

I'm trying to create an extension which makes an external API POST call.

First I tried to make a simple fetch call, but it is failing due to CORS policy:

const response = await fetch('https://api.example.com/data', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({key: 'value'})
});

But then in Block source I have found method unstable_fetchAsync(requestJson).

const requestJson = {
     method: 'POST',
     url: 'https://api.example.com/data',
     headers: [
         ["Content-Type", "application/json"]
     ],
     body: btoa(JSON.stringify({key: 'value'})),
     redirect: 'manual',
     integrity: null
}
await unstable_fetchAsync(requestJson);

It's working as expected, but the name is suspicious. Also, this method is not documented anywhere. I'm afraid that after creating the extension, I will not be able to submit it for public usage. 

Is this safe to use unstable_fetchAsync or there is another way to make external API POST calls?

0 Replies 0