0

我正在为Zapier中的intouch api进行集成。出于某种原因,API 设置body为在GET请求中而不是在参数中接收查询。

这一切都在 Postman 中工作,但似乎该z.request函数忽略了带有请求的body选项属性。GET

这是我的代码:

const test = (z, bundle) => {
  const query = {
    matterGuid: "bf508fcf-5f36-4191-93d6-fecd5a7f6a04",
    getFields: ["matter.reference"],
  };

  return z.request({
    method: "GET",
    url: baseUrl + "/matters",
    body: JSON.stringify(query), //I've tried body, json, raw, data, etc
  });
};

这是我收到的回复:

{
      status: 400,
      json: { message: 'No request data received', success: false, errors: [] },
      data: { message: 'No request data received', success: false, errors: [] },
      content: '{"message":"No request data received","success":false,"errors":[]}',
      request: {
        method: 'GET',
        headers: {
          'user-agent': 'Zapier',
          'x-intouch-o-token': 'xxxxxxxxxxxxxxxxxxx',
          'Content-Type': 'application/json; charset=utf-8'
        },
        url: 'https://demo.intouch.cloud/api/v2/public/matters',
        _addContext: [Function: addContext],
        input: {
          bundle: [Object],
          _zapier: [Object],
          _addContext: [Function: addContext]
        },
        merge: true,
        removeMissingValuesFrom: { params: false, body: false },
        replace: true,
        skipThrowForStatus: false,
        _requestStart: 2020-10-24T11:42:37.026Z
      },
      skipThrowForStatus: false,
      headers: Headers {
        [Symbol(map)]: [Object: null prototype] {
          date: [Array],
          'content-type': [Array],
          'content-length': [Array],
          connection: [Array],
          'set-cookie': [Array],
          'access-control-allow-origin': [Array],
          'access-control-allow-headers': [Array],
          'access-control-allow-methods': [Array],
          'x-content-type-options': [Array],
          'arr-disable-session-affinity': [Array],
          'x-frame-options': [Array],
          'strict-transport-security': [Array],
          'cf-cache-status': [Array],
          'cf-request-id': [Array],
          'expect-ct': [Array],
          'report-to': [Array],
          nel: [Array],
          server: [Array],
          'cf-ray': [Array]
        }
      },
      getHeader: [Function: getHeader],
      throwForStatus: [Function],
      _addContext: [Function: addContext]
    }
4

1 回答 1

0

也许这不是理想的解决方案,但我找到了解决方法。

我发现作为z.request方法中的中间件的一部分,它被显式地从请求中body删除。GET

// No need for body on get
  if (req.method === 'GET') {
    delete req.body;
  }

结果,我能够添加更多的中间件将主体放回原处。幸运的是,添加到before数组中的自定义中间件函数在 Zapier 自己的中间件之后运行,这意味着主体不受上述函数的影响。

这就是我想出解决这个问题:

const includeQuery = (request, z, bundle) => {
  if (request.method ==='GET'){
    if (bundle.inputData.matterGuid && bundle.inputData.getFields){
      const query = {
        matterGuid: bundle.inputData.matterGuid,
        getFields: bundle.inputData.getFields.split(','),
      };
      request.body = JSON.stringify(query);
    }    
  }

  return request;
};

然后将其添加到befores导出对象的数组中,如下所示:

  befores: [includeApiKey, includeQuery],
  afters: [handleBadResponses],

我真的很想知道是否有人对此问题有更好的解决方案。

于 2020-10-25T11:53:06.520 回答