【问题标题】:how to access payload from fastify-http-proxy in the prehandler hook如何从 prehandler 钩子中的 fastify-http-proxy 访问有效负载
【发布时间】:2022-06-14 04:24:12
【问题描述】:

我正在开发一个应用程序,我需要点击此端点https://api2.transloadit.com/assemblies 来上传任何类型的文件。 我希望在到达最终端点之前从服务器添加身份验证参数 请注意:我希望在预处理程序挂钩中编辑 multipart/formdata 这是我的代码:

fastify.register(require("fastify-http-proxy"), {
  upstream: "https://api2.transloadit.com/assemblies",
  undici: true,
  prefix: "/api", // optional
  http2: false,
  preHandler(request, reply, next) {
    // how to access the data in request body
    console.log(request);
    next();
  },
});

但这是 request.body 的输出:

body: IncomingMessage {
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: [],
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      sync: true,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      destroyed: false,
      errored: null,
      closed: false,
      closeEmitted: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: true,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: null
    },
    _events: [Object: null prototype] { end: [Function: clearRequestTimeout] },
    _eventsCount: 1,
    _maxListeners: undefined,
    socket: Socket {
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: null,
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 8,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: [Server],
      _server: [Server],
      parser: [HTTPParser],
      on: [Function: socketListenerWrap],
      addListener: [Function: socketListenerWrap],
      prependListener: [Function: socketListenerWrap],
      _paused: false,
      _httpMessage: [ServerResponse],
      [Symbol(async_id_symbol)]: 53,
      [Symbol(kHandle)]: [TCP],
      [Symbol(kSetNoDelay)]: false,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: null,
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(RequestTimeout)]: undefined
    }

【问题讨论】:

    标签: javascript api fastify pre fastify-nextjs


    【解决方案1】:

    如果您想将 auth 参数传递给第三方,您可以通过访问 url 的 request.raw 部分(如果它是基于 url 参数的 API 密钥)来注入它们。在你的情况下:

    fastify.register(require("fastify-http-proxy"), {
      upstream: "https://api2.transloadit.com/assemblies",
      undici: true,
      prefix: "/api", // optional
      http2: false,
      preHandler(request, _reply, next) {
        request.raw.url = `${request.raw.url}&apikey=${MY_API_KEY}`;
        console.log(request);
        next();
      },
    });
    

    如果身份验证参数是基于标头的,您实际上需要定义rewriteRequestHeaders 函数。这看起来像:

    fastify.register(require("fastify-http-proxy"), {
      upstream: "https://api2.transloadit.com/assemblies",
      undici: true,
      prefix: "/api", // optional
      http2: false,
      replyOptions: {
        rewriteRequestHeaders: (_, headers) => {
            return {
              ...headers,
              'x-api-key': MY_API_KEY
            };
          }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多