Call REST Service from Form

Is it possible to use a form for not adding data to a table, but just to gather data and send it to a REST service?

Let’s imagine an experiment: There is a trivial service like this to add numbers, https://api.val.town/v1/run/ralfw.Add?args=[41,1]. In a Glide app I want to give the user the option to add numbers A and B - and call this service behind the scenes. The UI could look like this:

How do I manage to do that? It seems, a form always needs to be attached to a table. That’s not, what’s needed here, but ok if inevitable.

It also seems, I need fields for the data to enter in this table, eg fields for A and B in this example. Well, that’s ok, too, if inevitable.

But what’s not working for me is, that a new row is always created when I press the submit button of a form.

I cannot hide the button, I cannot replace its action.

Or am I missing something?

How can I replace (!) the default action? (Or at least not add a new record to the table, but change the current record, eg the current user.)

Also, of course, how to display the result of a REST call? The form always closes when hitting submit, it seems. Or can I keep it open to display a result?

Why does it have to be a form? Couldn’t you use a detail screen or edit screen ?

I’m open to everything :slight_smile: But a detail/edit screen is attached to a table to and eager to add/store field content. Ok, an edit screen would just store it; that would be a step forward.

But how to get into edit mode right away from the menu or a tab icon?

How to get rid of the default buttons? (Just hide them? :thinking:)

I guess I’d add a custom button to trigger the REST call. Right?

Just use a single row helper table, with User Specific Columns for your inputs. This will allow multiple users to use it at the same time, all writing their own values to the same columns in the same row.
Then call the API using the Fetch JSON column.

2 Likes

I now was able to get rid of the add:

  1. Create a REST call table with columns for the params, eg A and B.
  2. Create a screen for that table.
  3. Delete the collection on the screen!!! // Very important step. That way only the first record gets shown/edited.
  4. Place entry fields for A and B on the screen.
  5. Add a JSON fetch column to the table and parameterize its URL with A and B.
  6. Show fetch column value on screen.

Whenever I change the values A or B the call is made and the updated result is shown.
No record gets added. All happens on the user specific fields of the first and only record in the table.

Great!

However… This is only a GET call. How to do a POST call and pass JSON into the REST service? Do I have to resort to a Javascript code column?

For now, yes. Or use Webhook + Make.
But… there is a new feature on the way that will allow you to make POST requests. Even authenticated ones, and do it securely. I don’t know exactly when it will land, but I believe it’s quite close.

A REST request action would be great!

In the meantime I used a JS column:

The REST call is triggered whenever A or B changes. That’s not, what I want - but ok for now.

I guess I could decouple data entry from REST call with a button and a “set value” action. The action could copy the value from A and B to “parameter columns” pA and pB which then are used by the JS column.

Let me try…

1 Like

Exactly - that would have been my next suggestion :slight_smile:

1 Like

oh, btw… I just noticed the console.error line in your JavaScript. Glide doesn’t support console logging (there is no console), so if the fetch ever fails the whole column will fail on the catch.

And it works very nicely :partying_face: The button just has to trigger “set values”. No custom action necessary.

What to do with errors during the JS function execution? (console.log does not work.) I guess I just would need to add an interpretation of the field value with an if-then column. Eg. if the col contains “error”, well, an error occurred, otherwise its a valid result. But that’s a detail.

1 Like

One more question: How can I display an hourglass or the like while (!) the REST call is running?
“Show notification” just pops up and disappears :frowning:

No real native solution for this, but what you could probably do is add an image component containing an animated GIF, and then set a visibility condition on that so that it’s only visible when the JavaScript column is empty. You might have to fiddle a bit with setting/clearing column values to get it to work as you want, but give that a go.

1 Like

Please use the new Call API column/action for this. You can enable it in Previews.

4 Likes

Link to announcement:

2 Likes

Turns out it was very close :rofl: :point_down:

1 Like

You should make claims like that more often so that we get more new features.

1 Like

1 Like

A great feature - but as it turns out not worth upgrading from the free plan I’m still using. Sorry.
My use case is not suffering from CORS issues, hence I’m happy with a client side JS fetch().
For now at least.

See my view on the free plan here.