【问题标题】:Zapier application: send body with GET requestZapier 应用程序:使用 GET 请求发送正文
【发布时间】:2021-02-07 06:49:20
【问题描述】:

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

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

这是我的代码:

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]
    }

【问题讨论】:

标签: node.js https zapier zapier-cli


【解决方案1】:

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

z.request 方法中的I discovered that as part of the middleware 中,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],

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

【讨论】:

  • 你的解决方案是正确的 - 根据this answerGET 请求的主体不应保存数据,应被服务器忽略,因此 Zapier 将其删除。
猜你喜欢
  • 2022-01-25
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
相关资源
最近更新 更多