Search code examples
node.jstrello

Trying to add a card to trello fails


I am trying to add a card to trello using a Node.js script. Other calls (all gets so far) to the trello system seem to be working, but this one just doesn't return any response at all. I tried plugging it into an API tester (postman) and I get back this error message:

This seems to be like an error connecting to http://URL: https://api.trello.com/1/cards?name=TestCard&pos=top&due=null&idlist=[listid]&key=[key]&token=[token]. The response status was 0. Check out the W3C XMLHttpRequest Level 2 spec for more details about when this happens.

Edit: Postman now returns the correct response (in 0.5s), with the change to capitalization called out in the comments below. However, my code times out after 3s. So something is wrong in the code after all.

This is the first Post call I've tried to implement, so that could be the problem here. My code looks like this:

var options = {
  hostname: api.trello.com, 
  port: 443,
  path: '/1/cards?name=TestCard&pos=top&due=null&idList=[listid]&key=[key]&token=[token]',
  method: 'POST',
  headers: { 'Content-Type': 'application/json'}
};
https.request(options, function(res) {
    var ResponseString = '';
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);
    //and more code here
});

The res.on('data') and res.on('end') functions both have console.logs and don't get called. The console.logs from this function don't get called either.

I have also tried the lists/[ListID]/cards?due=null&name=[CardTitle] url, without any response from that one either.

Is there something I need to know about posting from Node.js that I'm missing here?


Solution

  • https.request returns a ClientRequest object. You can use this object to add a body to the request (with req.write()). But, more importantly, the request isn't completed until you call req.end(); it expects that you haven't finished setting up the body yet if you fail to call that.