New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posting Arrays in Form #1538

Closed
pon opened this Issue Apr 10, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@pon

pon commented Apr 10, 2015

I am attempting to consume an API and am unable to make the following request:

curl https://api.stripe.com/v1/customers/{id}/bank_accounts/{id}/verify \
   -d amounts[]=14 \
   -d amounts[]=26

The below code doesn't work because having two keys of the same name the second overwrites the first. Is this type of request supported here?

request({
  method: 'POST',
  url: '...',
  form: {
    'amounts[]': 14,
    'amounts[]': 26
  }
})
@froatsnook

This comment has been minimized.

Show comment
Hide comment
@froatsnook

froatsnook Apr 10, 2015

Contributor

Since form in your example is a regular javascript object, you can't have two of the same key. Instead you should make amounts itself an array, like:

request({
  method: 'POST',
  url: '...',
  form: {
    'amounts': [14, 26]
  }
})

That will make amounts[0]=41&amounts[1]=26. If you need amounts[]=41&amounts[]=26, then use qsStringifyOptions like so:

request({
  method: 'POST',
  url: '...',
  form: {
    'amounts': [14, 26]
  },
  qsStringifyOptions: { arrayFormat: 'brackets' }
})
Contributor

froatsnook commented Apr 10, 2015

Since form in your example is a regular javascript object, you can't have two of the same key. Instead you should make amounts itself an array, like:

request({
  method: 'POST',
  url: '...',
  form: {
    'amounts': [14, 26]
  }
})

That will make amounts[0]=41&amounts[1]=26. If you need amounts[]=41&amounts[]=26, then use qsStringifyOptions like so:

request({
  method: 'POST',
  url: '...',
  form: {
    'amounts': [14, 26]
  },
  qsStringifyOptions: { arrayFormat: 'brackets' }
})
@pon

This comment has been minimized.

Show comment
Hide comment
@pon

pon Apr 10, 2015

Thanks! I misread the documentation - I thought that only applied to the query string

pon commented Apr 10, 2015

Thanks! I misread the documentation - I thought that only applied to the query string

@pon pon closed this Apr 10, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment