Skip to content

Commit

Permalink
Ensure formData value is not undefined
Browse files Browse the repository at this point in the history
One must be careful that all formData values are not undefined

An undefined value in form-data append will cause a confusing error:
`TypeError: Cannot read property 'name' of undefined`

Simple fix here is casting to empty string

To repro:
```js
var formData = { woops: undefined };
var request = require('request');
request('https://github.com', { formData }, console.log)
```

Related PR to fix form-data form-data/form-data#417
  • Loading branch information
fijimunkii committed Jan 25, 2019
1 parent df346d8 commit d93d502
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ Request.prototype.init = function (options) {
var formData = options.formData
var requestForm = self.form()
var appendFormValue = function (key, value) {
// cast undefined value to empty string otherwise form-data barks
if (typeof value === 'undefined') {
value = '';
}
if (value && value.hasOwnProperty('value') && value.hasOwnProperty('options')) {
requestForm.append(key, value.value, value.options)
} else {
Expand Down

0 comments on commit d93d502

Please sign in to comment.