• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to combine multiple cfhttp request to one variable?

Enthusiast ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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?

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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.

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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#" />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

Steve, it's getting close but it's not working correctly yet. It looks like instead of appending, it's replacing with the last/latest cfhttp calls instead of combining both cfhttp calls.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 12, 2016 Oct 12, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 13, 2016 Oct 13, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 19, 2016 Oct 19, 2016

Copy link to clipboard

Copied

When you do a structAppend, the second struct is appended to the first. The return value is just the status of the operation, a boolean.

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

<cfdump var="#structObj1#" label="structObj1 with structObj2 appended" />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 19, 2016 Oct 19, 2016

Copy link to clipboard

Copied

The issue here is that the structure keys have the same name so the append is either saving the first or the second copy, depending on the overwrite option. I'm thinking you want an array of structures as opposed to a single structure:

<cfset variables.results = [] />

<cfhttp...></cfhttp>

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

<cfhttp...></cfhttp>

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

...

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 19, 2016 Oct 19, 2016

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation