Feathers: REST 客户端:接受大量 GET 请求的数据或正文参数

创建于 2019-05-27  ·  3评论  ·  资料来源: feathersjs/feathers

你好,

我在 GET 请求中遇到了一些关于长 URL 的问题。 我需要在一些“查找”查询中传递一长串参数。

当前的解决方案是创建一个新的中间件和方法覆盖。

// allow x-http-method-override header for GET methods
exports.default = function (req, res, next) {
    if (req.headers['x-http-method-override'] === 'GET' && req.method === 'POST') {
        req.method = 'GET';
        req.query = req.body;
        delete req.body;
    }
    next();
};

然后,我像这样使用我的服务:

await awesomeService.create(
          {
             someParam: aVeryLongArray // will replace req.query server side thanks to the middleware, and thus bypass URL limitations of browsers
          },
          {
            headers: {
              "x-http-method-override": "GET" // tell the server that this is actually a GET request, so it triggers our middleware
            }
          }
        );

非常感谢 Sebastian 对 Slack 的帮助(抱歉找不到您的 GitHub 句柄)。

根据这个 SO answer,这是一个有效的模式,因为 POST HTTP 词不仅限于创建本身,而且还与任何其他类型的无法分类的操作相关联。 因此,可以将“大量参数化的查找”视为 POST 的相关用例。

但是......问题在于,在 Feathers 中,POST 请求与“创建”动作密切相关。 在使用 REST 客户端时,意味着我们必须使用create方法来查找数据,这很烦人,而在服务器端,这意味着在create挂钩上设置相关权限,这也是令人不安。

我建议在 GET 请求中支持bodydata字段(所以getfind方法)。 我认为实现是微不足道的(我很乐意 PR),但我想知道:

  • 如果这是个好主意(主要是关于浏览器,他们可以在获取请求中删除databody字段吗?)
  • 什么是最好的模式( body参数, data参数?)

最有用的评论

我一直在考虑让 REST 客户端更灵活一点,这样您就可以更轻松地实例化您自己的定制客户端服务。 允许使用 JSON 进行 POST 查询也将避免所有 URL 字符串转换和数组解析限制。 目前在客户端自定义 REST 服务如下所示(这也是您可以使用不同端点的方式):

const FetchService = require('@feathersjs/rest-client/lib/fetch');

class MyFetchService extends FetchService {
  get (id, params = {}) {
    if (typeof id === 'undefined') {
      return Promise.reject(new Error(`id for 'get' can not be undefined`));
    }

    return this.request({
      url: this.makeUrl({}, id),
      method: 'POST',
      data: params.query,
      headers: Object.assign({
        'x-http-method-override': 'GET'
      }, params.headers)
    }, params).catch(toError);
  }
}

app.use('/someservice', new MyFetchService({
  connection: window.fetch,
  name: 'someservice',
  base: 'mybaseUrl',
  options: {}
}));

一个非破坏性的改变可以让做

const feathers = require('@feathersjs/feathers');
const rest = require('@feathersjs/rest-client');

const app = feathers();

// Connect to the same as the browser URL (only in the browser)
app.configure(rest({
  base: '/',
  Service: MyFetchService
}));

所有3条评论

我一直在考虑让 REST 客户端更灵活一点,这样您就可以更轻松地实例化您自己的定制客户端服务。 允许使用 JSON 进行 POST 查询也将避免所有 URL 字符串转换和数组解析限制。 目前在客户端自定义 REST 服务如下所示(这也是您可以使用不同端点的方式):

const FetchService = require('@feathersjs/rest-client/lib/fetch');

class MyFetchService extends FetchService {
  get (id, params = {}) {
    if (typeof id === 'undefined') {
      return Promise.reject(new Error(`id for 'get' can not be undefined`));
    }

    return this.request({
      url: this.makeUrl({}, id),
      method: 'POST',
      data: params.query,
      headers: Object.assign({
        'x-http-method-override': 'GET'
      }, params.headers)
    }, params).catch(toError);
  }
}

app.use('/someservice', new MyFetchService({
  connection: window.fetch,
  name: 'someservice',
  base: 'mybaseUrl',
  options: {}
}));

一个非破坏性的改变可以让做

const feathers = require('@feathersjs/feathers');
const rest = require('@feathersjs/rest-client');

const app = feathers();

// Connect to the same as the browser URL (only in the browser)
app.configure(rest({
  base: '/',
  Service: MyFetchService
}));

嗨,我也有同样的问题,查找请求应该能够接受多个参数,并且我们会达到 URL 限制。 因此,我一直在努力查看是否可以进行 POST 和路由以“查找”而不是“创建”。

我尝试了@eric-burel 的建议,将 HTTP 请求方法类型从 POST 重写为 GET,但尝试在服务中查找“create”方法而不是路由到“find”时失败。 我验证了请求对象方法确实更改为“GET”,但羽毛路由可能发生在此更改之前?

app.use('/appointments', function(req, res, next) {
if (req.method === 'POST') { // 仅为此服务执行此操作。
req.method = 'GET';
req.query = req.body;
删除req.body;
}
下一个();
}, {
查找:函数(参数,回调){
// 做一点事
}
});

如果我在这里遗漏了什么,请你们澄清一下吗? 或者更好的是,让我知道是否有更好的方法来做到这一点?

这可以通过 v5 中的新自定义方法 (https://github.com/feathersjs/feathers/issues/1976) 来完成

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Mattchewone picture Mattchewone  ·  4评论

Vincz picture Vincz  ·  4评论

corymsmith picture corymsmith  ·  4评论

rstegg picture rstegg  ·  3评论

jordanbtucker picture jordanbtucker  ·  4评论