Skip to main content
2Charlie
Inspiring
October 12, 2016
Question

How to combine multiple cfhttp request to one variable?

  • October 12, 2016
  • 3 replies
  • 1511 views

I'm not sure if I explain this correctly but I want to do multiple cfhttp requests and then combine them into one cfset variable and just do one DeserializeJSON(variable). Is this possible? If so, how do I go about it?

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    October 20, 2016

    2Charlie wrote:

    I'm not sure if I explain this correctly but I want to do multiple cfhttp requests and then combine them into one cfset variable and just do one DeserializeJSON(variable). Is this possible? If so, how do I go about it?

    It is a puzzle why you would want to deserialize the result of a cfhttp call. Deserialization returns a complex object such as struct or array. But the result of a cfhttp is, in general, already a struct.

    In any case, what you want to do is possible. You could bend it like this,

    <cfhttp method="get" url="site1" result="res1">

    <cfhttp method="get" url="site2" result="res2">

    ...

    <cfhttp method="get" url="site5" result="res5">

    <cfset cfhttpContent.site1=res1>

    <cfset cfhttpContent.site2=res2>

    ...

    <cfset cfhttpContent.site5=res5>

    <cfset all5sitesJsoned = serializejson(cfhttpContent)>

    <cfdump var="#deserializejson(all5sitesJsoned)#">

    It might be more efficient to use a loop.

    Legend
    October 12, 2016

    Yes, it is doable but I would really need more info. You can do something as simple as:

    <cfset variables.results = [] />

    <cfhttp...></cfhttp>

    <cfset arrayAppend(variables.results,{ headers:cfhttp.headers, filecontent:cfhttp.filecontent }) />

    <cfhttp...></cfhttp>

    <cfset arrayAppend(variables.results,{ headers:cfhttp.headers, filecontent:cfhttp.filecontent }) />

    ...

    <cfoutput>#serializeJSON(variables.results)#</cfoutput>

    2Charlie
    2CharlieAuthor
    Inspiring
    October 12, 2016

    Thanks, Steve. What you're posting is almost exactly what I'm looking for. Instead of #serializeJSON(variables.results)#, I'm doing #DeserializeJSON(variables.results)# and it's giving me the "Error in custom script module".

    Legend
    October 12, 2016

    Then it sounds like you want something more along the lines of:

    <cfset variables.results = {} />

    <cfhttp...></cfhttp>

    <cfset structAppend(variables.results,deserializeJSON(cfhttp.filecontent),true) />

    <cfhttp...></cfhttp>

    <cfset structAppend(variables.results,deserializeJSON(cfhttp.filecontent),true) />

    ...

    <cfdump val="#variables.results#" />

    WolfShade
    Legend
    October 12, 2016

    Not only does asking the same question more than once in more than one thread not get you help any faster, but it tends to annoy the other users.  Please refrain from creating multiple posts/threads with the same question.

    ^_^

    2Charlie
    2CharlieAuthor
    Inspiring
    October 12, 2016

    Are you referring to this thread, How to setup cfhttpparam that takes multiple values?

    That thread was for accepting multiple parameters...more like accepting/retrieving values in comma delimited from dynamic parameters passing from the URL. And this thread is about multiple CFHTTP calls. Are they the same question? The API I'm using limits the number of results returned to only 5; therefore, I need multiple CFHTTP calls for additional results. If you think this is the same question then I'm sorry.

    WolfShade
    Legend
    October 12, 2016

    I apologize.  I saw "cfhttp" and "multiple" and erroneously thought you were posting the same question.

    ^_^