0

so I am trying to post to an API using Coldfusion with an array of emails.

For a quick over view of the API schema,

{
 "emails":["foo@bar.com", "bar@foo.com"],
 "orgId":1,
 "subject":"foobar",
 "body":"foobar",
 "sender":"foobar@com"
}

Now this is my coldfusion script

<cffunction name="inviteusers" access="public" returntype="any">
    <cfset var data = "" />
    <cfhttp url="urlserver#UserInvite" method="post" username="#username#" password="#urlpass#" result="data">
        <cfhttpparam name="emails"  this is array or emails>
        <cfhttpparam name="orgID" type="formType" value="1">
        <cfhttpparam name="body" type="formType" value="this is the body">
        <cfhttpparam name="sender" type="formType" value="hmm@yahoo.com">
    </cfhttp>
</cffunction>

The emails is where I am stuck at.. I'm not sure how to approach this problem..

Thank you guys!

jmesolomon
  • 563
  • 5
  • 15
  • Is the API actually expecting to receive JSON? If so, the above won't work. Otherwise, try using two `` tags, one for each address. What are the results? – Leigh Jan 26 '15 at 22:30
  • Hi @Leigh, can you kindly explain why the above won't work if the API expects JSON? Yes, the API receives and returns JSON. And if I did one for each addresses, that means that I would have to call this API according to the number of email addresses or do a foreach loop.. – jmesolomon Jan 27 '15 at 00:17
  • [JSON](http://json.org/) is a string in a specific format. The code above just sends the key/value pairs via a standard post. It won't automatically convert the data to JSON. So if that is what the receiving end is expecting, it probably will not work. Can you provide a link to the API docs? Most likely you will need to create a CF structure, with the various orgId, etcetera.. Then use `SerializeJSON`, but we need to see the API to offer more specific advice. – Leigh Jan 27 '15 at 04:03
  • @Leigh ah! I see, my bad. Sorry for incomplete details. I am using Jquery post with `datatype: 'json'` to `inviteuser` method to a controller class (working on MVC framework). I've created a Javascript obj to contain the data that I need, just as what you have explained above (just in Javascript hehe ^^).. Thank you very much Leigh! I've finally managed to answer this question.. I'll post it here – jmesolomon Jan 27 '15 at 04:21

1 Answers1

0

Allrighty,

here is how I answered this question. Since I am working on an MVC framework, in the view where I have the form, I've created a JQuery post function to the function in a controller.

I have an html input where a user can type in any number of emails separated by comma. I have a javascript function that takes the value of the input and splits them at 'comma' then add them to an array object.

Since the API is structured as this :

{
 "emails":["foo@bar.com", "bar@foo.com"],
 "orgId":1,
 "subject":"foobar",
 "body":"foobar",
 "sender":"foobar@com"
}

in my $.post() function I have an object for to be used as the data:

var myData = {
                "emails" : myEmails, //This myEmails is an array['fooBar@.com', 'barfoo@.com'] of emails from the single email input.
                "orgId" : 1,
                "subject" : $('#vgridSubject').val(),
                "body" : $('#vgridText').val(),
                "sender" : "foobar@ls.com"
            }

now in my controller, here is how I handle the array of emails

<cfscript>
  sendInvites = postUserInvite(emails = FORM['EMAILS[]'], orgID = FORM.orgID, subject = FORM.subject, body = FORM.body, sender = FORM.sender)
</cfscript>

Take note of FORM['EMAILS[]'] as this lets me access the values from form scope.

Link to the form array Working with Form Arrays in ColdFusion?

Community
  • 1
  • 1
jmesolomon
  • 563
  • 5
  • 15
  • Not sure I follow how that relates to cfhttpparam, but glad you figured something out. RE: *Take note of `FORM['EMAILS[]']`* Where in the code are you using the field name "emails[]" ? – Leigh Jan 27 '15 at 22:00