Search code examples
jsonapifetch-apizapier

Fetch POST to Tsheets API returns "code": 417, "message": "Expectation Failed: data field missing"


I am attempting to create a Zapier Zap to pull and push data from/to Tsheets via the Tsheets API. I have successfully managed to use the Fetch method to GET data from Tsheets. I am now trying to POST data and after a day of Googling and searching this site I have still not come up with a solution so am hoping you guys may be able to give me some leads.

Here is my POST Code:

var payload = '{data:[{name:"aServiceItem1", customfield_id:"118530", short_code:"c1" }, {name:"aServiceItem2", customfield_id:"118530", short_code:"c1"}]}';
var endpoint = "https://rest.tsheets.com/api/v1/customfielditems";

fetch(endpoint, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer <my_token>"
  },
  data: JSON.stringify(payload)
})
  .then(function(response) {
    return response.text();
  })
  .then(function(responsebody) {
    var output = {
      response: responsebody
    };
    callback(null, output);
  })
  .catch(function(error) {
    callback(error);
  });

In the Zapier zap development console, Test this Step it compiles without error and communicates with the Tsheets API however the response I get back is

response { "error": { "code": 417, "message": "Expectation Failed: data field missing" } }

As far as I can tell, according to the Tsheets API documentation here, the only mandatory items are "name" and "customfield_id" and as far as I can tell in my code I am supplying those. From that documentation there is a response item _status_extra which may supply more information for me but I'm not seeing that value in the response. Can I modify my code to supply me that value?

And an anyone see why my code is failing to POST my data?

Disclaimer: The POST code is not my own, I found it on this site.

TIA.


Solution

  • OK for the record this is what worked in the end.

    var payload =  { "data": [{"name": "From Zapier2", "customfield_id": "118530", "short_code": "FZ2"}]};
    
    var endpoint = "https://rest.tsheets.com/api/v1/customfielditems";
    
    fetch(endpoint, {
        method: "POST",
        headers: {"Content-Type": "application/json", "Authorization": "Bearer <MyToken>"},
        body: JSON.stringify(payload)
        })
          .then(function(response) {
            return response.text();
          })
          .then(function(responsebody) {
            var output = {response: responsebody};
            callback(null, output);
          })
        .catch(function(error) {
            callback(null, error);
        });