Browse Source

Detect formdata body and set content-type header (#220)

v5.x
Dylan Piercey 9 years ago
committed by Vsevolod Strukchinsky
parent
commit
3713879aaf
  1. 5
      index.js
  2. 1
      package.json
  3. 48
      readme.md
  4. 26
      test/headers.js

5
index.js

@ -259,7 +259,10 @@ function normalizeArguments(url, opts) {
opts.method = opts.method || 'POST';
if (isPlainObj(body)) {
if (isStream(body) && typeof body.getBoundary === 'function') {
// Special case for https://github.com/form-data/form-data
opts.headers['content-type'] = opts.headers['content-type'] || 'multipart/form-data; boundary=' + body.getBoundary();
} else if (isPlainObj(body)) {
opts.headers['content-type'] = opts.headers['content-type'] || 'application/x-www-form-urlencoded';
body = opts.body = querystring.stringify(body);
}

1
package.json

@ -65,6 +65,7 @@
"devDependencies": {
"ava": "^0.16.0",
"coveralls": "^2.11.4",
"form-data": "^1.0.1",
"get-port": "^2.0.0",
"get-stream": "^2.3.0",
"into-stream": "^2.0.0",

48
readme.md

@ -248,6 +248,54 @@ got('google.com', {
```
## Form data
You can use the [`form-data`](https://github.com/form-data/form-data) module to create POST request with form data:
```js
const fs = require('fs');
const got = require('got');
const FormData = require('form-data');
const form = new FormData();
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
got.post('google.com', {
body: form
});
```
## OAuth
You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to create a signed OAuth request:
```js
const got = require('got');
const OAuth = require('oauth-1.0a');
const oauth = OAuth({
consumer: {
public: process.env.CONSUMER_KEY,
secret: process.env.CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1'
});
const token = {
public: process.env.ACCESS_TOKEN,
secret: process.env.ACCESS_TOKEN_SECRET
};
const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
got(url, {
headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
json: true
});
```
## Unix Domain Sockets
Requests can also be sent via [unix domain sockets](http://serverfault.com/questions/124517/whats-the-difference-between-unix-socket-and-tcp-ip-socket). Use the following URL scheme: `PROTOCOL://unix:SOCKET:PATH`.

26
test/headers.js

@ -1,4 +1,5 @@
import test from 'ava';
import FormData from 'form-data';
import got from '../';
import pkg from '../package.json';
import {createServer} from './_server';
@ -49,6 +50,29 @@ test('zero content-length', async t => {
t.is(headers['content-length'], '0');
});
test.after('cleanup', async t => {
test('form-data manual content-type', async t => {
const form = new FormData();
form.append('a', 'b');
const headers = (await got(s.url, {
headers: {
'content-type': 'custom'
},
body: form,
json: true
})).body;
t.is(headers['content-type'], 'custom');
});
test('form-data automatic content-type', async t => {
const form = new FormData();
form.append('a', 'b');
const headers = (await got(s.url, {
body: form,
json: true
})).body;
t.is(headers['content-type'], `multipart/form-data; boundary=${form.getBoundary()}`);
});
test.after('cleanup', async () => {
await s.close();
});

Loading…
Cancel
Save