Zapier 应用程序:使用 GET 请求发送正文

Posted

技术标签:

【中文标题】Zapier 应用程序:使用 GET 请求发送正文【英文标题】:Zapier application: send body with GET request 【发布时间】: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]
    

【问题讨论】:

查看这个,我发现了为什么会这样。这是由于 Zapier 请求代码中的这一行:// No need for body on getif (req.method === 'GET') delete req.body;``github.com/zapier/zapier-platform-core/blob/master/src/… 【参考方案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 answer,GET 请求的主体不应保存数据,应被服务器忽略,因此 Zapier 将其删除。

以上是关于Zapier 应用程序:使用 GET 请求发送正文的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:为 Http GET 请求发送 JSON 正文

在 AXIOS 中为 GET 方法发送请求正文会引发错误

如何使用 HttpClient 在 GET 请求正文中发送内容?

是否可以在 Axios 中发送带有 JSON 正文的 get 请求?

在正文中发送 GET 请求参数

如何使用 axios get 请求发送正文数据和标头?