Redmine API with ColdFusion

A project I’m working on required a quick & easy way to add users to Redmine through the API, at first I thought it would be pretty simple, which it was, but it did give me a little headache.

From the Redmine API Docs they simply say to post a user hash table with the required information to /users.xml (or better yet .json!).  But a couple things came into play:
1: You must set the Content-Type to application/json
2: The user hash table can’t come in through url or form fields, it has to be serialized then posted to the body.
It took a long time searching google to find those two things out, since I can’t see it mentioned anywhere in the Redmine docs.

    var RedmineURL = 'foo.com/redmine/users.json';
    var postData = {
        user = {
            login = 'NewApiUser',
            password = 'password',
            firstname = 'My',
            lastname = 'Name',
            mail = 'user@localhost.com'
        }
    };
    var httpService = new http();
    httpService.setMethod("post");
    httpService.setUrl(RedmineURL);
    httpService.setUsername('dont_do_this');
    httpService.setPassword('use_an_api_key_instead');
    
    httpService.addParam(type="header",name="Content-Type",value="application/json");
    httpService.addParam(type="body",value=SerializeJson(postData));
    
    var result = httpService.send().getPrefix();
    writeDump(result);
    writedump( deSerializejson(result.filecontent.ToString()) );

Quite Note: I just wrote the code free-hand here, but I’m pretty sure it works.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

WordPress Appliance - Powered by TurnKey Linux