Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将post请求从request js转换为包含流参数的Get js_Javascript_Node.js_Requestjs_Gotjs - Fatal编程技术网

Javascript 如何将post请求从request js转换为包含流参数的Get js

Javascript 如何将post请求从request js转换为包含流参数的Get js,javascript,node.js,requestjs,gotjs,Javascript,Node.js,Requestjs,Gotjs,我正在将库从迁移到,并且遇到了一种特定类型的post请求。端点查找包含文件名和上载文件流的特定正文。我一直无法在got图书馆工作,现在正在寻求帮助 下面是代码片段或下面是 以下是工作呼叫请求: 您可以添加两个请求的完整标题吗?以及一段正文作为well@Anatoly在请求对象中,我只有上面列出的标题和正文。我已经对问题进行了编辑,以显示邮递员服务响应的请求。您是否可以使用Fiddler(在Windows上)或其他somilar实用程序(在Linux上)之类的工具捕获来自request和got的两

我正在将库从迁移到,并且遇到了一种特定类型的post请求。端点查找包含文件名和上载文件流的特定正文。我一直无法在got图书馆工作,现在正在寻求帮助

下面是代码片段或下面是

以下是工作呼叫请求:


您可以添加两个请求的完整标题吗?以及一段正文作为well@Anatoly在请求对象中,我只有上面列出的标题和正文。我已经对问题进行了编辑,以显示邮递员服务响应的请求。您是否可以使用Fiddler(在Windows上)或其他somilar实用程序(在Linux上)之类的工具捕获来自
request
got
的两个请求?您不需要使用
表单数据
包来指示正文中的表单数据。
/**
 * @param {string} uri 
 * @param {string} filename 
 * @param {stream} body file stream
 * @param {string} token
 * @returns {Promise<string>}
 */
async function uploadFile(uri, filename, body, token) {
  return await request({
    headers: {
      Accept: "application/json",
      Authorization: `OAuth2 ${token}`,
      ["Content-Type"]: "multipart/form-data"
    },
    formData: {
      source: body,
      filename
    },
    method: "POST",
    uri
  });
}
/**
 * @param {string} uri 
 * @param {string} filename 
 * @param {stream} body file stream
 * @param {string} token
 * @returns {Promise<string>}
 */
async function uploadFileTest1(uri, filename, body, token) {
  return await got(uri, {
    headers: {
      Accept: "application/json",
      Authorization: `OAuth2 ${token}`,
      ["Content-Type"]: "multipart/form-data"
    },
    form: {
      source: body,
      filename
    },
    method: "POST"
  });
}

/**
 * @param {string} uri 
 * @param {string} filename 
 * @param {stream} body file stream
 * @param {string} token
 * @returns {Promise<void>}
 */
async function uploadFileTest2(uri, filename, body, token) {
  const chunks = [];
  const pipeline = promisify(Stream.pipeline);
  await pipeline(
    body,
    got.stream.post(uri, {
      headers: {
        Accept: "application/json",
        Authorization: `OAuth2 ${token}`,
        ["Content-Type"]: "multipart/form-data"
      },
      form: {
        source: body,
        filename
      },
      method: "POST",
      isStream: true
    })
  ).catch(err => console.error(err.message));
}

/**
 * @param {string} uri 
 * @param {string} filename 
 * @param {stream} body file stream
 * @param {string} token
 * @returns {Promise<string>}
 */
async function uploadFileTest3(uri, filename, body, token) {
  const chunks = [];
  const pipeline = promisify(Stream.pipeline);
  const responseStream = new Stream.Duplex();
  await pipeline(
    body,
    got.stream.post(uri, {
      headers: {
        Accept: "application/json",
        Authorization: `OAuth2 ${token}`,
        ["Content-Type"]: "multipart/form-data"
      },
      form: {
        source: body,
        filename
      },
      method: "POST",
      isStream: true
    }),
    responseStream
  );
  return await new Promise((res, rej) => {
    responseStream.on('data', (chunk) => chunks.push(chunk));
    responseStream.on('error', rej);
    responseStream.on('end', () => res(Buffer.concat(chunks).toString('utf8')));
  }).catch((err) => {
    console.error(err.message, err.stack);
    throw new Error(err.message);
  });
}

/**
 * @param {string} uri 
 * @param {string} filename 
 * @param {stream} body file stream
 * @param {string} token
 * @returns {Promise<string>}
 */
async function uploadFileTest4(uri, filename, body, token) {
  const chunks = [];
  const pipeline = promisify(Stream.pipeline);
  const responseStream = new Stream.Duplex();
  await pipeline(
    got.stream.post(uri, {
      headers: {
        Accept: "application/json",
        Authorization: `OAuth2 ${token}`,
        ["Content-Type"]: "multipart/form-data"
      },
      form: {
        source: body,
        filename
      },
      method: "POST",
      isStream: true
    }),
    responseStream
  );
  return await new Promise((res, rej) => {
    responseStream.on('data', (chunk) => chunks.push(chunk));
    responseStream.on('error', rej);
    responseStream.on('end', () => res(Buffer.concat(chunks).toString('utf8')));
  }).catch((err) => {
    console.error(err.message, err.stack);
    throw new Error(err.message);
  });
}
{
  "args": {},
  "data": {},
  "files": {
    "test.txt": "data:application/octet-stream;base64,dGhpcyBpcyBhIHRlc3QgZG9jdW1lbnQ="
  },
  "form": {
    "filename": "text.txt"
  },
  "headers": {
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "postman-echo.com",
    "x-amzn-trace-id": "Root=1-5fad9989-0acbc1e97ad72aa26855df7b",
    "content-length": "346",
    "accept": "application/json",
    "authorization": "OAuth2 abc",
    "content-type": "multipart/form-data; boundary=--------------------------347114605132179681480756"
  },
  "json": null,
  "url": "https://postman-echo.com/post"
}