【问题标题】:API GET or Zapier trigger to POST actionAPI GET 或 Zapier 触发 POST 操作
【发布时间】:2016-09-01 21:53:59
【问题描述】:

我实际上要做的是在将卡片放在 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);

【问题讨论】:

    标签: javascript python json zapier harvest


    【解决方案1】:

    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 )
    

    【讨论】:

    • 我已经修改了您的代码以使用 Harves 运行,但它无法在邮递员之外运行。
    猜你喜欢
    • 2018-12-27
    • 2021-08-01
    • 1970-01-01
    • 2014-08-30
    • 2015-12-14
    • 2021-06-20
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多