Meteor HTTP.post not working with Trello API

為{幸葍}努か 提交于 2019-12-08 05:24:58

问题


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.



来源:https://stackoverflow.com/questions/22391082/meteor-http-post-not-working-with-trello-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!