0

We had a form which was submitting the data to a page in a new tab. Like,

<form name='formOne' action='/action.cfm' method='post' target='_blank'>
    <input type='hidden' name='employee' value='{"first_name": "test","last_name":"name"}' />
    <input type='hidden' name='contact' value='{"phone": "1233214090","fax":"1098760982"}' />
    <input type="submit" />
</form> 

But now "action.cfm" page is expecting a JSON value in http request body. Like

{
    "employee": {
        "first_name": "test",
        "last_name": "name"
    },
    "contact": {
        "phone": "1233214090",
        "fax": "1098760982"
    }
}

Not sure how could we send the JSON data in http request body in form post in this case. Please suggest if it is possible to do so or if there is any other approach to achieve this.

rrk
  • 15,677
  • 4
  • 29
  • 45
B S Nayak
  • 170
  • 1
  • 12
  • 1
    I just re-read your question... I think you need to clarify how the form is submitted. Are you no longer using the form that you show? – Redtopia Oct 15 '18 at 16:46
  • 2
    Unfortunately there is no official way to submit JSON (yet). Your best bet is using JavaScript to serialize the form data and send it via AJAX. – Alex Oct 15 '18 at 20:35
  • I agree with what @Alex said. See this post for a similar example - https://stackoverflow.com/a/9142943/1636917 – Miguel-F Oct 15 '18 at 20:38
  • Is /action.cfm used by other scripts in your app and can't be changed? What does /action.cfm do with the received JSON? – SOS Oct 17 '18 at 15:05

2 Answers2

2

In ColdFusion, this is how you send json in the body of a post request:

string function postAsJson(
    required struct data) {

    var responseStr = "";

    try {

        var http = new http(argumentCollection={
            "method": "post",
            "timeout": 50,
            "encodeUrl": false
        });

        http.addParam(type="body", value=serializeJSON(Arguments.data));
        http.addParam(type="header", name="content-type", value="application/json");
        http.setURL("your form handler");

        var httpResult = http.send().getPrefix();

        if (httpResult.status_code == 200) {
            responseStr = httpResult.fileContent;
        }

    } catch (any err) {
        responseStr = "<p>#err.message#</p>";
    }

    return responseStr;
}

myData = {
    "this": "and",
    "that": true
};

result = postAsJson(myData);
writeOutput(result);

And in your request handler, you get the data like this:

requestData = getHttpRequestData();
if (isJSON(requestData.content)) {
    myData = deserializeJSON(requestData.content);
    writeDump(myData);
}
else {
    writeOutput("<p>Invalid request</p>");
}

(I have not tested this in ACF, but I know that it does work in Lucee - 5.2.x)

Redtopia
  • 4,947
  • 7
  • 45
  • 68
  • Will it redirect to that form handler page? Because the user needs to be able to browse that page. – B S Nayak Oct 16 '18 at 06:52
  • The user cannot navigate to the request handler and send json to the body at the same time. If you need to do something after the post request, like display something, you could return that from the form handler and display it inline somewhere. I've updated the answer to reflect that. – Redtopia Oct 16 '18 at 17:58
0

To keep it within ColdFusion, you could obtain a JSON as follows, on the action page:

<cfif structKeyExists(form, "employee")><!--- Then form has been submitted --->
    <cfset employeeData = serializeJSON(form)>
</cfif>
BKBK
  • 484
  • 2
  • 9