Node.js - Array is converted to object when sent in HTTP GET request query

后端 未结 3 1849
情书的邮戳
情书的邮戳 2021-01-27 21:10

The following Node.js code:

var request = require(\'request\');

var getLibs = function() {
    var options = { packages: [\'example1\', \'example2\', \'example3         


        
3条回答
  •  礼貌的吻别
    2021-01-27 21:45

    This problem can be solved using Request library itself. Request internally uses qs.stringify. You can pass q option to request which it will use to parse array params.

    You don't need to append to url which leaves reader in question why that would have been done.

    Reference: https://github.com/request/request#requestoptions-callback

    const options = {
      method: 'GET',
      uri: 'http://localhost:3000/package',
      qs: {
        packages: ['example1', 'example2', 'example3'], 
        os: 'linux', 
        pack_type: 'npm' 
      },
      qsStringifyOptions: {
        arrayFormat: 'repeat' // You could use one of indices|brackets|repeat
      },
      json: true
    };
    

提交回复
热议问题