5
votes

I'm new to Coldfusion and can't understand why I'm having so much trouble adding a couple of headers to an http request that I'm making with cfhttp. I'm trying to write the request using cfscript, and from everything I've read I should be able to simply do:

httpService.addParam(type="header", name="Content-Type", value="application/json");

or

httpService.addParam(type="header", name="Authorization", value=local.authPasscode);

But the outgoing request doesn't seem to contain these headers. I can tell it's not working because:

  1. I'm getting errors back in my response
  2. I'm doing a writeDump(GetHttpRequestData()), which shows all the details of the request (and this output does not show the Authorization header at all and shows the Content-Type header as:

multipart/form-data; boundary=----WebKitFormBoundaryZs9TyOQV02N3fQop

I also seem to have difficulty adding the body of the message in a similar way so I'm thinking I'm doing something wrong with addParam(). I'm attempting that like so:

httpService.addParam(type="body", value="hello");

Again, the response I get back has a bunch of missing field errors and the output of writeDump(GetHttpRequestData()) shows a missing body.

Am I supposed to be able to use addParam() in this way?

Full code:

    httpService = new http();
    httpService.setMethod("POST");
    httpService.setUrl(application.config.beanstream.postURL);

    local.authPasscode = "Passcode " & ToBase64(application.config.beanstream.merchantid & ":" & application.config.beanstream.APIPasscode, "utf-8");

    // Set headers.

    httpService.addParam(type="header", name="Authorization", value=local.authPasscode);
    httpService.addParam(type="header", name="Content-Type", value="application/json");

    // Construct the message body.

    local.body = {
        "test": "hello"
    };

    httpService.addParam(type="body", value=SerializeJSON(local.body));

    writeDump(GetHttpRequestData());

    local.result = httpService.send().getPrefix();

Update:

Okay, so I changed my postURL to that of another local page and on that page I'm using GetHttpRequestData() to log the method, protocol, headers and content all out to a file. Here's what I get:

POST
HTTP/1.1
{host={www.mysite.com},user-agent={ColdFusion},connection={close},content-length={16},authorization={Passcode Mjc1ODMwMDAwOjIwMTI5NGUwMjI2MzQxMzlBZjBFMDE2RmViRjg0RDAz},content-type={application/json}}
{"test":"hello"}

So it looks to me like I am indeed correctly sending a POST to the correct URL and the headers are getting set correctly as well. My content even seems like it's there so this is starting to feel like I'm sending invalid body data to the API. Unless anybody can think of anything else to check I'm going to investigate that further.

1
What version of coldfusion?Cmacu
Thanks for asking that... I thought I was on 11 but it looks like I'm on 10. server.coldfusion.productverion reads 10,0,15,292620 specifically.Brad Marshall
Adding headers like that works fine for me, though I don't see anything when I use GetHttpRequestData() because that is just showing me the GET request that I'm making to the page that sends the POST request. Can you debug from your postURL to dump out the contents of the POST request?Mark Leiber
What is the HTTP response status code coming back from the API?William Greenly

1 Answers

0
votes

instead of

local.body = {
    "test": "hello"
};

try

local.body = {};
local.body["test"] = "hello";

then you can

SerializeJSON(local.body)