How to define pass POST Data via a UX REST Data Broker?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2021 03:37 AM
I am trying to call a POST-based REST API via UX REST Data Broker from UI Builder.
I have managed to get a POST call working with no data
called in UI Builder via
api.data.approve_task.execute({
taskId: api.context.props.taskId
});
However I'm not clear on how to pass POST data into this via the `execute` method. The UI Builder API Documentation (execute) only indicates how to do this for URL parameters, but only specifies this for a simple operation with no Data.
I have tried:
1) adding as a "dataParam" (as used in createHTTPEffect)
api.data.approve_task.execute({
taskId: api.context.props.taskId
dataParam: {
comment: "my comment"
}
});
> this results in "no data" in REST client script i.e. the data isn't passed through
2) Looks like I can "hard-code" a "Request Body" in the Data Broker definition
> which get through to the REST client script - but isn't what I'm after
3) defined "data" as a parameter and tried to refer to it as
> but that results in "com.glide.rest.util.RESTRuntimeException: The payload is not valid JSON." in the logs
Any ideas on how to pass the data from client, though the Data Broker and into REST endpoint?
I have a follow on question - "if you execute the data resource, from UI Builder, how do you get access to the return data?"
"execute" doesn't return a Promise, nor does it seem to make a blocked call in the script - as logging the data via "api.data.approve_task.output" returns undefined - even though I see the data returned in Developer Tools (albeit base64 encoded)
- Labels:
-
Now Experience UI Framework
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2021 07:24 AM
I have come to the conclusion that the Data Broker solution for REST doesn't support POST Data being passed from client-side "api.data.<resource>.execute()" through into HTTP request/response resources.
Obviously if anyone knows different - happy to be otherwise convinced!
To get round this, I have cloned the logic from my REST resource script into a Script Include and got data passed in via a "Data Broker Server Script" (sys_ux_data_broker_transform) - which is a whole lot simpler.
On the followup question (which still applies to other Data Brokers) - UI Builder shows on "Events" tab on the Data Broker, so output can be processed in seperate client script etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2022 08:33 PM
We are looking at a Data Transform broker and it works for calling any server flow REST api but now running into how to pass request object to the data broker - it looks like it only takes a data resource or a client state parameter but the issue is client states trigger it prematurely - we need to call .Refresh() with parameters from a button click event.
Has anyone figured out how to call a REST POST API with a request object from UI Builder Button Click event? There has to be an easy way besides messing with client state async parameters.
thanks
Marty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2022 03:28 AM
Matt, I'm not sure which you are referring to - "Data Transform Broker" ("sys_ux_data_broker_transform") or "REST Data Broker" ("sys_ux_data_broker_rest").
I eventually managed to get a REST POST working, but (as per my comment above) have moved to using Data Transform Broker in preference.
However, I think the key for not "triggering prematurely" is to set the "Mutates Server data" on the server-side (not UI Builder definition). This should mean they show in UI Builder as "Explicit" only on the "when to evaluate" field.
Then you should be able to call it in client script via:
api.data.<data-resource>.execute({
field1: value,
field2: value2
});
To process the response you will need to link another Client Script (event handler) to the data resource "Operation Succeeded" event and then handle `event.payload"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2021 07:38 PM
Hi, The below configuration works.
URL
/api/now/table/{{tableName}}
Request Body
{short_description: "{{short_description}}", caller_id: "{{caller_id}}"}
Here are my props ( in data resource).
[
{
"name": "tableName",
"label": "Table name",
"description": "table",
"readOnly": false,
"fieldType": "string",
"mandatory": true,
"defaultValue": "incident"
},
{
"name": "short_description",
"label": "Short description",
"description": "Short description",
"readOnly": false,
"fieldType": "string",
"mandatory": true,
"defaultValue": "incident"
},
{
"name": "caller_id",
"label": "Callen Id",
"description": "Caller Id",
"readOnly": false,
"fieldType": "string",
"mandatory": true,
"defaultValue": "incident"
}
]
Here is my script to create (post) incident record.
api.data.data_source_id.execute({
tableName: "incident",
short_description: "Something wrong in my network",
caller_id: api.context.session.user.sys_id
});
Hope this helps.
thanks