How to use the post function from request

Find comprehensive JavaScript request.post code examples handpicked from public code repositorys.

request.post is a function in Node.js that sends an HTTP POST request to a specified URL with optional form data, JSON payload, or other options.

217
218
219
220
221
222
223
224
225
226
post(peer:any, uri:string, data:any) {
  if (!peer.isReachable()) {
    return Promise.resolve();
  }
  return new Promise((resolve, reject) => {
    const postReq = request.post({
      "uri": protocol(peer.getPort()) + '://' + peer.getURL() + uri,
      "timeout": this.timeout || constants.NETWORK.DEFAULT_TIMEOUT
    }, (err:any, res:any) => {
      if (err) {
fork icon77
star icon0
watch icon1

30
31
32
33
34
35
36
37
38
39
newArticle.discuss = 0;
newArticle.status = 0;
return await this.articleRepository.save(newArticle).then(res => {
    if (process.env.DEV === 'production') {
        // 百度 seo push
        request.post({
            url: `http://data.zz.baidu.com/urls?site=${process.env.BAIDU_PUSH_SITE}&token=${process.env.BAIDU_PUSH_TOKEN}`,
            headers: {'Content-Type': 'text/plain'},
            body: `${process.env.BAIDU_PUSH_SITE}/article/${newArticle.id}`,
        }, (error, response, body) => {
fork icon37
star icon139
watch icon3

+ 7 other calls in file

How does request.post work?

request.post is a function provided by the request library in Node.js used to send an HTTP POST request to a specified URL with optional form data, JSON payload, or other options. When request.post is called, it takes as input a URL string and an optional options object containing data, headers, authentication, and other configuration options. If the options object contains form data, it is encoded as a URL-encoded string and sent as the request body. If the options object contains a JSON payload, it is serialized as a JSON string and sent as the request body with a Content-Type header of application/json. The request.post function returns a http.IncomingMessage object that represents the server's response. This object provides methods for reading the response data and headers, as well as properties indicating the response status code and message. request.post also supports streaming and piping of request and response data, allowing for efficient handling of large files and streams of data. request.post is commonly used in Node.js applications to send data to a server, such as submitting a form, uploading a file, or interacting with an API. By providing a simple and flexible interface, request.post makes it easy to work with HTTP requests and responses in Node.js.

40
41
42
43
44
45
46
47
48
49
});
socket.on('close', function() {
        sockets--;

        Object.keys(hosts).forEach(function(host) {
                request.post(host + '/gc', noop);
        });

        gc();
});
fork icon25
star icon245
watch icon23

+ 3 other calls in file

150
151
152
153
154
155
156
157
158
159
    [global.AUTH_NAME]: bkTicket,
    allow_overwrite: 'true' // 覆盖原有的源码包
}
const url = `${v3Config.URL_PREFIX}/bkapps/applications/${appInfo.appCode}/modules/${appInfo.moduleCode}/source_package/link/`
return new Promise((resolve, reject) => {
    request.post({ url, formData }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            resolve(body)
        } else {
            let res = {}
fork icon32
star icon31
watch icon9

+ 2 other calls in file

Ai Example

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
const request = require("request");

const options = {
  url: "https://example.com/api",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: {
    name: "John Smith",
    age: 30,
    email: "john.smith@example.com",
  },
  json: true,
};

request.post(options, (error, response, body) => {
  if (error) {
    console.error(error);
    return;
  }

  console.log(response.statusCode);
  console.log(body);
});

In this example, we first import the request module. We define an options object that contains the URL of the server, the HTTP method (in this case, POST), a Content-Type header indicating that the request body is JSON, and a JSON payload with some data. We set the json option to true to indicate that the payload should be serialized as JSON. We then call request.post with the options object and a callback function that will be called when the server responds. If there is an error, we log it to the console. Otherwise, we log the HTTP status code and the response body to the console. When we run the code, it will send a POST request to the specified URL with the JSON payload. The server will process the request and send a response, which will be logged to the console.

27
28
29
30
31
32
33
34
35
36
let user;
let confirmation_code;

describe('Register test', function() {
  it('should be successful', function(done) {
    request.post(
      {
        url: url('/api/auth/register'),
        json: sampleUser
      },
fork icon24
star icon0
watch icon2

+ 13 other calls in file

3
4
5
6
7
8
9
10
11
12
const http = require('http');
const utils = require('./utils');
const url = require('url');

const oldRequestGet = request.get;
const oldRequestPost = request.post;
const oldHttpsRequest = https.request;
const oldHttpRequest = http.request;

function createException() {
fork icon17
star icon48
watch icon2

+ 9 other calls in file

64
65
66
67
68
69
70
71
72
73
prompt.message   = "";
prompt.delimiter = "";

console.log(" Please Enter Your GitHub Credentails ".yellow.inverse);
prompt.get(promptSchema, function(err, credentials) {
  request.post('https://api.github.com/authorizations', {
    headers : {
      'User-Agent': 'Pullr NPM v'+package.version
    },
    'auth': {
fork icon6
star icon34
watch icon42

+ 3 other calls in file

75
76
77
78
79
80
81
82
83
84
var self = this;
var url =  self._getUrl(UPLOAD_PATH) + '?' + self._getRequestQueryString(options);

if (options.image) {
    // already got the raw image data, just post it
    request.post({ url: url, body: options.image }, callback);
    return;
}

if (!options.imagePath) {
fork icon2
star icon9
watch icon2

+ 9 other calls in file

12
13
14
15
16
17
18
19
20
21
    UnknowError: 127
}

function postAsync(_path, json) {
    return new Promise((resolve, reject) => {
        request.post(`${secret.pixivpyUrl}${_path}`, {
            json
        }, (err, res, body) => {
            if (err || body.code != 0) {
                if (body.code == -1) reject(errorCode.LoginFail);
fork icon21
star icon115
watch icon6

146
147
148
149
150
151
152
153
154
  form: {
    response: captchaResponse,
    secret: secret
  }
};
request.post(postData, function (err, response, body) {
  if (err) {
    return done(err);
  }
fork icon31
star icon4
watch icon0

34
35
36
37
38
39
40
41
42
      "Accept": accept,
      "Content-type": "application/x-www-form-urlencoded"
    },
  };

  request.post(options).pipe(res);
});

router.post('/api/publish', protector.protect(true), async function (req, res, next) {
fork icon4
star icon14
watch icon20

+ 110 other calls in file

36
37
38
39
40
41
42
43
44
45

it('allows to send finish times', (done) => {
  startBlocks.add('0','test Block');
  participants.save(aParticipant)
    .then(() => {
      request.post({url: url, headers: headers, form: form}, (err, response) => {
        expect(response.statusCode).toBe(200);
        done();
      });
    })
fork icon17
star icon12
watch icon8

+ 7 other calls in file

223
224
225
226
227
228
229
230
231
232
  },
  'headers': {
    'authorization': authorizationHeader
  }
};
request.post(config.get('previews.url'), params, function(err, response, body) {
  if (err) {
    log.error({
      'err': err,
      'id': id,
fork icon7
star icon13
watch icon0

58
59
60
61
62
63
64
65
66
67
    body: JSON.stringify(payload)
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";


request.post(options, function(err, response, body) {

    // var loginString = printf("login  -s " + config.server + " -u " + config.username + " -p '" + password + "' --realm " + config.realm);
    // call(loginString, config, function(err, stdout) {
    if (err) {
fork icon7
star icon4
watch icon4

442
443
444
445
446
447
448
449
450
451
    headers : {
        'Authorization' : "Basic " + Buffer.alloc(self.options.username + ":" + self.options.password).toString('base64')
    }
};

request.post(data, (err, resp, body) => {
    if (err) {
        callback(err);
        return;
    }
fork icon3
star icon5
watch icon0

298
299
300
301
302
303
304
305
306
307
    'Authorization': 'Basic ' + (new Buffer(serverData.clientID + ':' + serverData.clientSecret).toString('base64'))
    },
    json: true
};

request.post(authOptions, function(error, response, body) {
    if (!error && response.statusCode === 200) {

    var access_token = body.access_token,
        refresh_token = body.refresh_token;
fork icon2
star icon7
watch icon0

+ 3 other calls in file

33
34
35
36
37
38
39
40
41
42
43
 * @returns {Promise}       Promise object represents the post body
 */


const postShopify = function(option) {
  return new Promise(function(resolve, reject) {
    request.post(option, function(err, res, body) {
      if (err) {
        reject(err);
      }

fork icon1
star icon5
watch icon2

1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
    gzip: true,
};

log.info("Making a request to " + url);
log.debug("body=" + post.body);
request.post(post, function (err, response, body) {
    log.info("Received response.");
    if (
        !err &&
        (!response ||
fork icon2
star icon2
watch icon3

+ 9 other calls in file

66
67
68
69
70
71
72
73
74
};

Buildbot.prototype.sendChanges = function(options, user, callback) {
  var body = this._sendChangesBody(options, user);
  var change_url = this.url(this._changeHookURL);
  request.post({uri: change_url, form: body}, function (error, response, body) {
    callback(error, response, body);
  });
};
fork icon0
star icon1
watch icon52

766
767
768
769
770
771
772
773
774
775
} else {
  this.log.debug(body);
  this.log.error("No Token received visiting url and accept the permissions.");
  this.log.info(getRequest.uri.href);
  const form = this.extractHidden(body);
  getRequest = request.post(
    {
      url: getRequest.uri.href,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
fork icon1
star icon5
watch icon3

+ 10 other calls in file