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".

    2Charlie
    2CharlieAuthor
    Inspiring
    October 13, 2016

    I found this documentation and I tried the "Merging structures using Coldfusion" example and it's not working either.

    <cfset structObj1 = structNew() />

    <cfset structObj1.key1 = "struct1, key1" />

    <cfset structObj1.key2 = "struct1, key2" />

    <cfdump var="#structObj1#" label="structObj1" />

    <cfset structObj2 = structNew() />

    <cfset structObj2.key1 = "struct2, key1" />

    <cfset structObj2.key3 = "struct2, key3" />

    <cfdump var="#structObj2#" label="structObj2" />

    <cfset result = structAppend(structObj1, structObj2) />

    <cfdump var="#structObj1#" label="structObj1" />

    <cfoutput>structAppend() was successful: #result#<br/></cfoutput>

    If I dump structObj1 and 2 indvidually then it works but if I tried to dump result, it just show "YES" and not the two structures combined.


    I found this documentation from Adobe, Adobe ColdFusion 9 * StructAppend , and if I set the second structAppend to false, then it only shows the data from the frist structAppend. If set it to true then the second structAppend overwrites the first. So, what did I do wrong?

    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.

    ^_^