Disable automatically added square brackets at end of array name

Open
#46 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
ruby
Domain
api

Research direction

Start at the Unirest.post call and trace how the parameters hash serializes array values for the request body. Confirm the desired behavior with the examples in the issue: brackets should not be added automatically, while an explicitly named key such as "data[]" should remain usable.

Written by the indexing model from the issue text.

Description

Say I want to post an array of data to a remote source:

    array = [1,2,3]
    Unirest.post "https://www.my-website.com", parameters: { data: array }

When I receive the HTTP request on my-website.com, the body of the POST looks like this: data[]=1&data[]=2&data[]=3

According to this StackOverflow post, those square brackets are a convention to "address a limitation of PHP, which doesn't generate an array automatically if multiple values with the same name are submitted". All well and good if I'm posting to a PHP server, but if I'm posting to a different kind of server (in my case NancyFx), I can't easily get that data into an array.

I think it would make sense to get rid of these brackets by default and allow developers using unirest-ruby to post to a PHP server to do something like this:

    array = [1,2,3]
    Unirest.post "https://www.my-website.com", parameters: { "data[]": array }
Dominant language
Ruby
Stars
364
Forks
76
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Kong/unirest-ruby

All issues in Kong/unirest-ruby

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.