2

I tried to send post request to API and the post parameters should be array, this is how to send it in cURL

curl http://localhost:3000/check_amounts
  -d amounts[]=15 \
  -d amounts[]=30

I tried to do that in Node.js using request module

request.post('http://localhost:3000/check_amounts', {
        form: { 
                'amounts[]': 15 ,
                'amounts[]': 30
              }
    }, function(error, response, body) {
        console.log(body)
        res.json(body);
    });

but the second amount override the first one and the API gets the result as following: amounts = [30]

Then I tried to send it in different way

 request.post('http://localhost:3000/check_amounts', {
            form: { 
                    'amounts[]': [ 15 , 30]
                  }
        }, function(error, response, body) {
            console.log(body)
            res.json(body);
        });

but the result was not as an expected amounts = [{"0":15},{"1":30}]

Note : the header should contains 'Content-Type': 'application/x-www-form-urlencoded' not 'application/json'

Does any one have solution to this problem?

2
  • Did you try: 'amounts': [ 15, 30 ] ? Commented Apr 1, 2015 at 8:16
  • yes ... it will be considered as object ... it didn't work Commented Apr 1, 2015 at 8:18

1 Answer 1

5

It's quite easy if you read the manual of request. All you should do is replace the form by querystring rather than object, in your case this should be:

amounts=15&amounts=30

the only thing I'm not sure is the above expression works in your web server. As I know it works well in java struts. So if not you may try amounts[]=15&amounts[]=30 instead. Hope it help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.