1

I am migrating a library from request to got and am getting stuck with a specific type of post request. The endpoint looks for a specific body which contains a filename and a stream of the file being uploaded. I have been unable to get this working in the got library and am now seeking assistance.

Below are the code snippets or here is a link to a repl.it version

Here is the working call in request:

/**
 * @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
  });
}

Here are my unsuccessful tests using got:

/**
 * @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);
  });
}

Here is the request from https://postman-echo.com service:

{
  "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"
}
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
APW
  • 420
  • 4
  • 15

0 Answers0