0

In my CFM file, I have a form that I need to send it as JSON. This is my first time trying to send a form as JSON data. I have looked at the example in W3C HTML JSON form submission but I do not see an example on how to specify the json URL. I saw that the form's enctype attribute has to be set to application/json but that's pretty much it. I need to know where I specify the jsonp URL, authkey, and other parameters that are required to submit to the proper location. Since I'm using ColdFusion, I'm not sure if I should continue using ColdFusion tags for JSON submission or Javascript and/or JQuery. Any suggestion is much appreciated. By the way, the authkey needs to be private since this is a post. Below is my form I've gotten so far. enter image description here

The remote API service accepts the data as follows. So, I need some way in the form to specify these criteria.

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
        )
)

Okay, looks like I have to build the JSON string before I do a form submission to itself and do a CFHTTP tag to send the JSON data. Below is what I have so far:

<cfset jsonString ={
                    data = {
                        article_id = "#artID#",
                        type = "comment",
                        project_id = "55c4ffd123131c527e294fe6",
                        content = "#form.message#",
                        public_name = "#form.name#",
                        public_email = "#form.mailfrom#"
                    }
                }>
<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>

However, it's not working. I'm still not totally clear on what the CFHTTPPARAM should be since now I am manually building all the parameters as a string instead of submitting them individually inside the CFHTTP tag. Any help is much appreciated.

Community
  • 1
  • 1
2myCharlie
  • 1,587
  • 2
  • 21
  • 35
  • What happens now if you submit the form and dump what you get? – Dan Bracuk Mar 17 '16 at 16:03
  • I have not tried that because I'm not sure how to dump a form. – 2myCharlie Mar 17 '16 at 16:12
  • I guess this is what I'm trying to do in this thread: http://stackoverflow.com/questions/8932973/how-to-post-json-data-to-remote-api-using-coldfusion-cfhttp – 2myCharlie Mar 17 '16 at 16:16
  • However, I think that thread does not present a form for the user like I am trying to. So, it's a bit different from my issue. – 2myCharlie Mar 17 '16 at 16:18
  • Okay, so I thought maybe the only way to do it is to submit the form to another page and from that page, grab all the values in the form and do a cfhttp post to the API. – 2myCharlie Mar 17 '16 at 16:34
  • To dump form, post it to a .cfm page and use the cfdump tag or writedump function. – Dan Bracuk Mar 18 '16 at 13:13
  • I am able to dump the JSON string but I'm not sure about the cfhttpparam since now I am prebuilt the JSON string versus submitting them inside of the cfhttp tag individually. I'm not sure what "name" attribute I need to call. – 2myCharlie Mar 21 '16 at 22:51

0 Answers0