完全回复身体的反应 [英] Resend body from response exactly as it comes

查看:173
本文介绍了完全回复身体的反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方API Rest,它返回正文中文件的内容。这个文件可以是文本或二进制文件(pdf,docx)。

I'm using a 3rd party API Rest which returns the contents of a file in the body. This file can be text or binary (pdf, docx).

出于安全考虑,我需要使用中间API Rest作为我的前端应用程序和第三方之间的桥梁API Rest。

For security reasons I need to use an intermediate API Rest as a bridge between my frontend app and this 3rd party API Rest.

我想要的是能够返回从第三方到我的前端应用程序的完全相同的身体,因为此刻我得到的身体并在我的中间API中构建一个新的响应我在某种程度上修改了一些东西。

What I want is to be able to return the exact same body that I get from the 3rd party to my frontend app, because at the moment as I get the body and build a new response in my intermediate API I'm somehow modifying something.

这是我在我的中间API中所做的:

This is what I do in my intermediate API:

const options = {
  method: 'GET',
  uri: `${api}`,
  headers: { OTCSTICKET: ticket}
}

rp(options)
  .then(parsedBody => res.status(201).send(parsedBody))
  .catch(err => res.status(400).send({ msg: 'download error', err }));

我需要发送与响应完全相同的正文。我怎么能这样做?

I would need to send exactly the same body that I get in the response. How can I do that?

谢谢

推荐答案

好我管理为了让这个工作,所以我会在这里发布,以防它帮助某人。

Ok I managed to get this working, so I'll post here in case it helps someone.

这个帖子给了我解决方案
使用请求获取Node.js中的二进制内容

This thread gave me the solution Getting binary content in Node.js using request

所以我只是将编码设置为null,然后我从第三方API获得了一个身份

So I just set the encoding to null and I passed as body the one I got from the 3rd party API

const options = {
  method: 'GET',
  uri: `${api}`,
  headers: { OTCSTICKET: ticket},
  encoding: null,
  resolveWithFullResponse: true
}

rp(options)
  .then(response => res.status(201).send(response.body))
  .catch(err => res.status(400).send({ msg: 'download error', err }));

有了这个,我设法让这个工作。

With this I managed to get this working.

这篇关于完全回复身体的反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆