API GET或Zapier触发POST操作

时间:2016-05-06 18:13:24

标签: javascript python json zapier harvest

我实际上要做的是在将一张卡放入Trello的列表中时在Harvest中生成发票。我试过Zapier,但内置了发票功能。

我需要自己开发。 Trello有一个Javascript或Python动作,所以我只限于这两种语言。

ZAPIER: Trello Trigger > Javascript or Python code

我有需要发送到

的JSON请求
https://[site].harvestapp.com/invoices
Authorization: Basic amNtMjU4MkBnbWFpbC5jb206YTEwMDUw**NB
Content-Type: application/javascript
Accept: application/json

{ "invoice": { "due_at_human_format": "NET 10", "client_id": 3849315, "currency" : "United States Dollar - USD", "issued_at": "2015-04-22", "subject": "Your invoice subject goes here", "notes": "Some notes go here", "number": "303197", "kind": "project", "projects_to_invoice": "120353", "import_hours": "yes", "import_expense": "yes", "period_start": "2015-03-01", "period_end": "2016-03-31", "expense_period_start": "2015-03-31", "expense_period_end": "2016-03-31" } }

如何使用基本的登录身份验证(包括逻辑)在 bare-bones python或JavaScript中发布此内容?一些示例代码会有所帮助。

更新: 我添加了这段代码,但似乎无法让它在Postman之外工作

var data = JSON.stringify({
  "invoice": {
    "due_at_human_format": "NET 10",
    "client_id": 3849315,
    "currency": "United States Dollar - USD",
    "issued_at": "2015-04-22",
    "subject": "Your invoice subject goes here",
    "notes": "Some notes go here",
    "number": "303197",
    "kind": "project",
    "projects_to_invoice": "120353",
    "import_hours": "yes",
    "import_expense": "yes",
    "period_start": "2015-03-01",
    "period_end": "2016-03-31",
    "expense_period_start": "2015-03-31",
    "expense_period_end": "2016-03-31"
  }
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://[url].harvestapp.com/invoices");
xhr.setRequestHeader("authorization", "Basic amNtMjU4MkBnbWFpbC***TEwMDUwMTNB");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "2c652344-1be5-8969-adf3-a7ca9ee7179f");

xhr.send(data);

1 个答案:

答案 0 :(得分:0)

Trello拥有完整的REST API,因此您不仅限于Python或JavaScript,除非您使用Zap设置的限制 - 这可能是Zapier所能实现的主要建在Django上。

话说回来,用两种语言发起POST请求都很容易。这是一个适用于您的JavaScript示例,但您必须对其进行调整以满足您的特定需求。

var request = new XMLHttpRequest();
var url = 'https://[site].harvestapp.com/invoices'; // or whatever your url actually is
var headers = {
    Authorization: 'amNtMjU4MkBnbWFpbC5jb206YTEwMDUw**NB',
    Content-Type: 'application/javascript',
    Accept: 'application/json'
};
var params = { // your JSON encoded data };

request.open( 'POST', url, true );
request.setRequestHeader( headers );
request.send( params );

在Python中它看起来像这样:

import requests
url = 'https://[site].harvestapp.com/invoices'
request.headers = {
    'Authorization': 'amNtMjU4MkBnbWFpbC5jb206YTEwMDUw**NB',
    'Content-Type': 'application/javascript',
    'Accept': 'application/json'
}
data = {} # put your data there instead of an empty dictionary
request.post( url, data )