forwardemail / forwardemail/superagent

Set Content-Type for multipart fields

Open
#1,062 2 comments 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
16.6k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Superagent uses `form-data` underneath for multipart forms, which accepts different data types (String, Buffer, Stream) for each part. `form-data` in addition allows to specify custom headers for each of the parts (espeically `Content-Type`). In case of files, `form-data` inserts the `Content-Type` header according to their MIME types. But using superagent, there is no way to specify `Content-Type` or other headers for `String` or `Buffer` types.

```
-------------324902384238
Content-Disposition: name=somefield
Content-Type: application/json

{ "msg": "someMessage" }
-------------324902384238--
```

The fix in superagent will be very simple: (lib/request_base.js)

```
exports.field = function(name, val, opts) {
this._getFormData().append(name, val, opts);
return this;
};
```

And use it as follows:

```
.field('somefield', '{}', { contentType: 'application/json' })
```

Relevant code in `form-data`: https://github.com/form-data/form-data/blob/master/lib/form_data.js#L229

Is there any plans to support this? I would love to contribute.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.