POST to API

Just going around in circles trying to understand how to use the fetch JSON column to make a POST request to an API.

I struggle a lot with the syntax, and to be honest I don’t even know if it’s possible. But does anybody know what I need to do to get this test example from the API docs to work?

curl --location --request POST 'https://api.dibsy.one/v1/payments' \
--header 'Authorization: Bearer sk_test_42069ce69ea8686921f128be69d65ddf9706' \
--header 'Content-Type: application/json' \
--data-raw '{
   "description":"Gold Style Watch - Special Edition",
   "amount":56.99,
   "metadata":{
      "product_id":156,
      "customer_id":345
   },
   "customer":{
      "name":"John Doe",
      "email":"your_customer@email.com",
      "phone":"+97433333333"
   },
   "redirectUrl":"https://example.com/order"
}'

Here are the docs:

https://docs.dibsy.one/api-reference/payments-api/create-payments

The Fetch JSON column uses a GET request, and cannot use Bearer-style authorization.

This column can also be called arbitrarily often and repeatedly, so it’s not safe to use it to trigger actions like making payments.

The Webhook action can make POST requests.

1 Like

Fair enough!

I use A LOT of webhooks to do things with Integromat.

But using that API docs example POST request, how would I dial that into the webhook action so that it posts straight to the Dibsy API?

There’s HTTP module in Integromat. It can make POST requests. So you could first send a webhook from Glide to Integromat and then use HTTP module to make request to the API you want.

4 Likes

This :point_up_2:

I’m aware of that, I was thinking it was possible to do it from Glide to cut out the middle-man. Nevermind, back to plan B!

you can do it in Google scripts… and use webhook to update a sheet that is connected to your Glide App:

PP avatar background stripe avatar background

2 Likes

Hola @david

Just a couple of technical questions about JavaScript libraries that Glide uses in their plugins: Fetch JSON & JavaScript.

I know we cannot make POST requests by using the Fetch JSON column but our plan B can be a custom JS code and run it via JavaScript plugin. It’s is fine but sometimes I have problems to understand and find the correct syntax within all the JS libraries and flavors available around the world. :roll_eyes:

I read time ago that Glide uses Lodash as JS library and also, we can import other JS libraries but I have a doubt about the Fetch() function and the right/best syntax to use.

As you know, Fetch() API supports the Promise object and helps a lot to retrieve/handle data and intercept any error during the execution of the request.
For example, this simple code uses Promise objects:

fetch('https://yesno.wtf/api') 
  .then(response => response.text())
  .then(result => console.log(result)) 

and returns these values using text format (.text()):
`

{“answer”:“no”,“forced”:false,“image”:“https://yesno.wtf/assets/no/1-c7d128c95c1740ec76e120146c870f0b.gif”}

but the above code cannot be used in Glide, the JS plugin doesn’t support it and we must change it using this syntax:

var Response = await fetch('https://yesno.wtf/api')
const Result= await Response.text();
return Result

Glide shows the result like (all is ok)

image

As you can imagine, if I try to run my Glide code in other JS editors, nothing works :face_with_raised_eyebrow:

So, my questions are:

  1. Which JS library can I import into Glide in order to work/use Fetch API’s Promise objects?
  2. If I want to use Bearer authorization in Glide’s JavaScript plugin, which JS library do I have to import/use?
  3. If I want to test my Glide JS code on other online platforms like JSFiddle, what language do I have to choose to get right results?. I tested it using “TypeScript” but no luck!

image

Thanks David, saludos!

@gvalero very good start, but it’s clear that you are just starting to learn JavaScript! Please do not mention me for help with code-I do not have time to help you with it. Good luck!

Thanks and sorry for tagging you. I just wanted to know a little more about a point you mentioned earlier.

The Fetch JSON column uses a GET request, and cannot use Bearer-style authorization.

but anyway, I still try to understand all JavaScript flavors when I need to create a bit more complex code and use the correct function. :innocent:

Taking your example code, this won’t run in Glide JavaScript plugin (I think)

The plugin needs a fetch request with 2 “await” calls, something like this:

 let response = await fetch(url, options);  // resolves with response headers
 let finalResult = await response.json();        // read body as json or text ( .text())
 return finalResult 

but, if the code does not use “await” calls (like your example), no result is shown in JS column:

fetch(url, options)
  .then(response => response.json())     // read body as json or text ( .text())
  .then(finalResult => console.log(finalResult));

this was my original intention of asking you.
Why the Glide JavaScript plugin is not able to work with API Fetch without using "await” calls?. Can I import another library to get it?

Sorry again for the inconvenience, felíz noche David!

All of that code can run inside Glide, if you understand what the await calls are doing and how JavaScript promises work. I’m sorry, I do not have time to help you with it.