Forum Discussion

akshays's avatar
2 years ago

Making multiple http.client.request third-party API requests in a single endpoint

We are integrating a third part REST API and there is a need to make multiple calls to their endpoint.

Example code: 

 

<#assign query1 = "sampleQuery"/>

<#assign resp1=http.client.request("https","${url}","/services/data/v54.0/").
parameter("q","${query1}").
header("Authorization","Bearer "+"${accesstoken}").header("Content-Type","application/json").get() />

<#if resp1.hasError>
<#else>
    <#assign sampleNumber = 4/>
</#if>

<#if sampleNumber > 2>

    <#assign query2 = "sampleQuery"/>
    <#assign resp2=http.client.request("https","${url}","/services/data/v54.0/").
    parameter("q","${query2}").
    header("Authorization","Bearer "+"${accesstoken}").header("Content-Type","application/json").get() />

    <#if resp2.hasError>
    <#else>
        ${result}
    </#if>

<#else>
</#if>

 

In this sample code, we are making a GET request, fetching certain data and then using it to make another GET ( or PATCH, POST ) request further on.

Technically, this does produce correct results. But, would this technique cause any concerns? Are all the calls made using http.client.requests synchronous? Or, should one endpoint be used to make only one request?
  • MattV's avatar
    MattV
    Khoros Staff

    http.client.request is not synchronous, so the code will stop executing until some sort of response from the server, or a timeout. 

    I'm not sure I've made two http.client.request calls in one endpoint, but I'm not sure I see any major issues with doing that. Though if one or both http.client.request calls take a long time, it's possible the execution time of the endpoint exceeds the cloudfront timeout, so you might get a HTTP 504 Gateway Timeout error.

    Obviously, if your 2nd request depends on results of the first, you'll want to make sure you validate the response of the first before you make the 2nd to avoid unnecessary calls. 

  • *Not an engineer so bit over my head* - We've been integrating ChatGPT, and I only opened this topic because I recognize the subject line as it's the same we've had open with a bug forever 🙂    -  I imagine this has to be what we are doing with ChatGPT and making multiple requests.  In our case, ChatGPT times out like crazy, so we had to extend the timeout with Khoros during testing.  So, my only real help is, if you do happen to need to extend the timeout for any testing as Matt notes above could be an issue, just escalate your case immediately because Support told us repeatedly they were extending it, and that everything was our fault, when in reality they didn't make the change properly, and it took 5 weeks of back and forth (and case escalation) until they caught their mistake ğŸ¤•