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

Is this how to send json data?

Enthusiast ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

I'm trying to send JSON data via CFHTTP. Is this all I need or do I need to do anything else? I want make sure I'm not missing anything.

<cfhttp url="https://app.kb.com/api/head/comment.json" method="post" timeout="15" throwonerror="true" >

  <cfhttpparam type="url" name="_authbykey" value="56ec1f0123131c78636142d6">

   <cfhttpparam type="url" name="project_id" value="55c4ffd123131c527e294fe6">

   <cfhttpparam type="url" name="article_id" value="#artID#">

  <cfhttpparam type="url" name="content" value="#form.message#" />

   <cfhttpparam type="url" name="public_name" value="#form.name#" />

   <cfhttpparam type="url" name="public_email" value="#form.mailfrom#" />         

</cfhttp>

Thank you!

Views

4.4K

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
Guide ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

Does it work?

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 ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

I got no errors but I am unable to retrieve the data after I've sent it.

<cfhttp url="https://app.kb.com/api/head/comment.json" method="post" timeout="15" throwonerror="true" >

  <cfhttpparam type="url" name="_authbykey" value="56ec1f1232131c78636142d6">

     <cfhttpparam type="url" name="project_id" value="55c4ffd123131c527e294fe6">

     <cfhttpparam type="url" name="article_id" value="#artID#">

     <cfhttpparam type="url" name="content" value="#serializeJSON(form.message)#" />

     <cfhttpparam type="url" name="public_name" value="#serializeJSON(form.name)#" />

     <cfhttpparam type="url" name="public_email" value="#serializeJSON(form.mailfrom)#" />

</cfhttp>

I've tried the serializeJSON() but I'm not sure if that's all I need. I've read this thread at StackOverFlow and it seemed that I've to build the string myself. However, that thread was about two years ago. I wonder if there is any changes in ColdFusion that does not require me to build the string myself.

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
Guide ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

You need to send everything as a JSON string inside of an <cfhttpparam type="body">.  This StackOverflow post may help: http://stackoverflow.com/questions/8932973/how-to-post-json-data-to-remote-api-using-coldfusion-cfht...

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 ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

Okay, this is what I have so far but I feel like it's not right.

<cfset jsonString ={

          "fields" = {

          "article_id" = "#artID#"

          },

          "fields" = {

          "type" = "comment"

          },

          "fields" = {

          "project_id" = "55c4ffd123131c527e294fe6"

          }

          "fields" = {

          "article_id" = "#artID#"

          },

          "fields" = {

          "conent" = "#form.message#"

          },

          "fields" = {

          "public_name" = "#form.name#"

          },

          "fields" = {

          "public_email" = "#form.mailfrom#"

          }

         }>

  <cfif cgi.request_method eq "post">

  <cfhttp url="https://app.kb.com/api/head/comment.json" method="post" timeout="15" throwonerror="true" >

     <cfhttpparam type="body" name="body" value="#serializeJSON(jsonString)#" />

  </cfhttp>

This is the comment object API that my JSON data needs to be in.

stdClass Object
(
  [valid] => 1
  [data] => stdClass Object
  (
  [id] => 54f126ed7cb82991627b23f0
  [type] => comment
  [project_id] => 54b88f0ffe775aac57000001
  [article_id] => 54b88f12bfe5a1601400001c
  [author_id] => 52d9a8dbfe775ad80d000001
  [content] => I'm a comment!
  [public_name] =>
  [public_email] =>
  [user_submitted] => 1
  [date_created] => 02/27/2015 9:24 pm EST
  )
)

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
Guide ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

There's a couple of typos in your <cfset> code: "conent" instead of "content", and "article_id" is in there twice (but maybe one of them was supposed to be "author_id"???).

Before sending the jsonString in your <cfhttp>, try dumping it out on your page to see if it is actually in the correct "structure" to match the API.  At the ColdFusion level, I think you want a single struct called "fields" with each of the fields as sub-structs. The whole thing needs to be wrapped in an outer struct too, but you've already got that part by defining jsonString as a struct.

Take a closer look at that StackOverflow post I linked to previously, particularly the answer that got 12 votes.  I think it will point you in the right direction.

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 ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

Okay, I cleaned up some more. For some reason, I kept getting Element MESSAGE is undefined in FORM. Looks like it's having issue retrieving data from the form when it's in the CFSET tag. However, when it's in the cfhttpparam or other place, it's fine. Am I doing this right?

<cfset jsonString ={

          "fields" = {

          "article_id" = "#artID#",

          "type" = "comment",

          "project_id" = "55c4ffd123131c527e294fe6",

          "content" = "#form.message#",

          "public_name" = "#form.name#",

          "public_email" = "#form.mailfrom#"

          }

         }>

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 ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

I figured out the error but I'm not sure about the <cfhttpparam type="body"> tag. Since all the parameters are combined into one string, how do I specify the CFHTTPPARAM with type="body"? Do I need the "name" attribute? And if so, what should it be?

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 ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

LATEST

The is what jsonString looks like when I dump:

{

    "DATA": {

        "PUBLIC_NAME": "testUser",

        "CONTENT": "This is my test comment...#6",

        "PROJECT_ID": "55c4ffd123131c527e294fe6",

        "ARTICLE_ID": "56c606e123131cb759485c6c",

        "PUBLIC_EMAIL": "user@mysite.com",

        "TYPE": "comment"

    }

}

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