createHttpEffect - parameter for body

Tommy Jensen
Giga Guru

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?

1 ACCEPTED SOLUTION

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.

View solution in original post

15 REPLIES 15

Thank you Brad. For now I will use this type of architechure as it actually is better since the call I need to do require authentication with userid/password so better to let the server handle that. 

I am still interested in knowing how to set the body in case I come across a use case where that is needed. 

 

This is what I am working on (very rougth and basic)
https://vimeo.com/420419736

Satyanarayana C
ServiceNow Employee
ServiceNow Employee

Hi,

Please try the below:

For example, you want to make a rest api call on click of a button

<now-button
    label="Submit"
    variant="primary"
    on-click={e => {
        dispatch('SAVE_REQUEST', 
                 {
                     requestData: 
                        {
                            status: "requested", 
                            user: properties.user.value, 
                            start_date: properties.startDate, 
                            end_date: properties.endDate
                        }
                }
        )
    }}
/>

 

In the action handler, you can do the following:

 

'SAVE_REQUEST': createHttpEffect(
        '/api/now/table/<table_name>',
        {
            method: 'POST',
            dataParam: 'requestData',
            successActionType: 'HTTP_EFFECT_SUCCESS',
            errorActionType: 'HTTP_EFFECT_ERROR'
        }
    )

Thank you for your effort. But you are really not answering my question. My question has nothing to do with updating a record via the table API. My question is about building a json body to send to an external REST API.

Hi,

The 'requestData' is a JSON payload which is being passed to the dataParam via the dispatched action. I was showing table API as an example, but I think you can call any rest api here.

requestData: 
                        {
                            status: "requested", 
                            user: properties.user.value, 
                            start_date: properties.startDate, 
                            end_date: properties.endDate
                        }
                

Thanks

No I am sorry but you are wrong.

What you suggest will not result in a json body. I have tried this approach. This datParam is a ServiceNow specific feature.

You can try it yourself.

Goto https://putsreq.com/ and create a fake endpoint and test your code out. You will then see that there is NO body on the request sent.