question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot make tests with post request and file upload

See original GitHub issue

Hi,

I’d like to make some stress test on my nodejs server with post requests that send files on the server. Here is the code I’m testing with:

config:
  target: 'https://websi.te'
  phases:
    - duration: 10
      arrivalRate: 10

scenarios:
  - flow:
    - get:
        url: "/"
    - post:
        url: "/upload/"
        formData: 
          photos: '@./file.jpg'
          token: 'myToken'

The error I have on my server is the following:

(node:27684) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 100): TypeError: Cannot read property 'token' of undefined

I have also an error 500 if I remove the token and its requirement from the server.

Note that this NodeJs Request code is working:

var options = { method: 'POST',
  url: 'http://websi.te/upload',
  headers: 
   { 'postman-token': '4997a0b0-71c8-61cf-c434-2b458dd4320c',
     'cache-control': 'no-cache',
     'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWwktrZu0gW' },
  formData: 
   { photos: 
      { value: 'fs.createReadStream("file.jpg")',
        options: { filename: 'file.jpg', contentType: null } },
     token: 'myToken' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);
  console.log(body);
});

Thanks for your help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
feimosicommented, Sep 14, 2017

I had the same problem and finally made it working. Here’s the example:

Config file:

config:
  target: 'http://localhost:8080'
  phases:
    - duration: 1
      arrivalRate: 1
  processor: './processor-utils.js'
scenarios:
  - flow:
    - post:
        url: '/upload/ocr'
        beforeRequest: 'setJSONBody'
        afterResponse: 'logResponse'
        formData:
          email: 'test@gmail.com'

processor-utils.js

const fs = require('fs');

module.exports = {
  setJSONBody,
  logResponse,
}

function setJSONBody(requestParams, context, ee, next) {
  const formData = {
    fileOCR: fs.createReadStream(__dirname + '/files/ocr.png'),
  };

  requestParams.formData = Object.assign({}, requestParams.formData, formData);

  return next();
}

function logResponse(requestParams, response, context, ee, next) {
  console.log('[DEBUG] Response: ', response.body);
  return next();
}
0reactions
ooiicommented, Jul 12, 2017

Can you point me to an example please? Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to send POST request in jmeter which have file upload ...
I am using Parameters for JSON Data and File Upload for file uploading in JMeter. Also used HTTP header manager for content type...
Read more >
How to unit test a POST request that has an uploaded file
How to unit test a POST request that has an uploaded file. I'm setting up a route that will take a file upload,...
Read more >
Testing file uploads with Postman (multipart/form-data)
I will show you how to debug an upload script and demonstrate it with a tool called Postman that can make requests encoded...
Read more >
Postman Tutorial - Upload a file with POST Request - YouTube
In this video we will learn different ways to upload a file with a Post Request in PostmanFound this video interesting - Please...
Read more >
POST method uploads - Manual - PHP
Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work. The global $_FILES will contain all the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found