
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2020 05:22 AM
I want to build a component to retrieve data but the rest api requires some parameters to be passed as json in the request body.
I am looking at the documentation for createHttpEffect and I cannot figure out how to send a json body with the request.
Does anybody know what parameters this should be passed in?
Solved! Go to Solution.
- Labels:
-
Now Experience UI Framework

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 02:09 PM
Hi Tommy,
I'll do a little digging and see if I can find something outside of the docs on developer. That being said, one of the things we demonstrated on last week's shows was using a Flow Designer action to consume an external API, then calling that action from a scripted rest API, and consuming the SRAPI from our component. That way we can abstract things from the component a bit and do any heavy lifting in the SRAPI. If you are looking to consume an external API from your component, that might be an architecture to look at either way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 05:50 AM
Hi,
Please verify below:
1) I have created a component that calls a scripted rest api
2) I am able to get the response back correctly.
Component code:
import { createHttpEffect } from '@servicenow/ui-effect-http';
import '@servicenow/now-button';
import '@servicenow/now-heading';
const view = (state, {dispatch, updateState}) => {
const { properties } = state;
return (
<div>
<now-button
label="Submit"
variant="primary"
on-click={e => {
dispatch('SAVE_REQUEST',
{
requestData:
{
status: "requested",
user: 'Test User'
}
}
)
}}
/>
<now-heading label={ properties.response } level="1" variant="header-primary" />
</div>
)
};
createCustomElement('sn-hello-test', {
renderer: {type: snabbdom},
view,
properties: {
response: { default: 'Click on submit to get response'}
},
actionHandlers: {
'SAVE_REQUEST': createHttpEffect(
'/api/now/test_json/test',
{
method: 'POST',
dataParam: 'requestData',
successActionType: 'HTTP_EFFECT_SUCCESS',
errorActionType: 'HTTP_EFFECT_ERROR'
}
),
'HTTP_EFFECT_SUCCESS': (coeffects) => {
console.log(JSON.stringify(coeffects.action.payload));
coeffects.updateProperties({response: JSON.stringify(coeffects.action.payload)});
}
}
});
Scripted REST API:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var payload = request.body.data;
var result = {};
result.postedMessage = payload;
response.setContentType("application/json");
response.setBody(result);
return response;
})(request, response);
Output before calling the rest api:
Output after clicking on submit:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2021 08:15 AM
can you please refer to this thread and help me out? :
https://community.servicenow.com/community?id=community_question&sys_id=5ecd70e0db8801d0ae81250913961970
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 07:56 AM
Glad to find this information, would give a try!
I'm not sure if we can use createHttpEffects to call external APIs, haven't tried yet but maybe for security you can only call APIs that are in your ServiceNow instance.
Probably one approach is that your component could call an API in ServiceNow that would also call another API that deals with the external API through another script but one caveat there is that there would be a delay in getting a response and also on how to handle errors.
There is one example of HTTP Effect that might work where it uses await fetch probably we can create custom methods to call another APIs in our component

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 08:30 AM
You can call external api's with createHttpEffects. I used https://putsreq.com/ to fake an api in order to check if the sent request has a body. That is why I know that the approach suggested by satyach will in fact not work with external API's. It works for internal because it has been designed specifically to do that.
But I am using the approach suggested by Brad and leave the external call to be performed by the backend and call scripted rest api with createHttpEffects. This way you also avoids having to deal with keys and passwords in the client.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2021 08:16 AM
can you please refer and help me out?
https://community.servicenow.com/community?id=community_question&sys_id=5ecd70e0db8801d0ae81250913961970