Meteor HTTP.post not working with Trello API

2019-06-06 09:59发布

问题:

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?

回答1:

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) {...});


回答2:

Try using data instead of params:

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


回答3:

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.



标签: meteor trello