Meteor HTTP.post not working with Trello API

2019-06-06 10:12发布

I'm trying to create a webhook through the Trello API by using Meteor's HTTP.post method like this:

HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
    params: {
        idModel: '...',
        callbackURL: '...'
    },
}, function(error, result) {...});

The request works but the response i get is "Invalid value for idModel". However, if i try the same request using jQuery:

$.ajax({
    type: 'POST',
    url: https://api.trello.com/1/webhooks?key=...&token=...,
    data: {
        idModel: '...',
        callbackURL: '...'
    },
});

Everything works fine (i.e. the webhook is created and data is returned). Somehow Meteor's method seems to make it impossible for Trello to parse the idModel field. Any ideas what might be behind this? Am i doing something wrong or is there a bug?

标签: meteor trello
3条回答
Anthone
2楼-- · 2019-06-06 10:37

I solved it by setting the Content-Type header to application/x-www-form-urlencoded. It was sent as text/plain before.

HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
  params: {
    idModel: '...',
    callbackURL: '...'
  },
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}, function(error, result) {...});
查看更多
做自己的国王
3楼-- · 2019-06-06 10:48

Try using data instead of params:

HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
  data: {
    idModel: '...',
    callbackURL: '...'
  },
}, function(error, result) {...});
查看更多
够拽才男人
4楼-- · 2019-06-06 10:51

Supplementing this page because I had the same problem doing this in golang. I solve it by adding

req.Header.Add("Content-Type", "application/json")

It is confusing that the 400 message is about an invalid idModel.

查看更多
登录 后发表回答