forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path07-post-data.js
40 lines (32 loc) · 1016 Bytes
/
07-post-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Example showing how to send POST data using the `POSTFIELDS` option.
*/
const querystring = require('querystring')
const { Curl } = require('../dist')
const curl = new Curl()
const url = 'http://httpbin.org/post'
const data = {
//Data to send, inputName : value
'input-arr[0]': 'input-arr-val0',
'input-arr[1]': 'input-arr-val1',
'input-arr[2]': 'input-arr-val2',
'input-name': 'input-val',
}
curl.setOpt(Curl.option.URL, url)
//You need to build the query string,
// node has this helper function for that:
curl.setOpt(Curl.option.POSTFIELDS, querystring.stringify(data))
curl.setOpt(Curl.option.VERBOSE, true)
curl.on('end', (statusCode, body) => {
console.log('Body received from httpbin:')
console.log(body)
curl.close()
})
curl.on('error', curl.close.bind(curl))
curl.perform()