2

I am sending a multidimensional array using the post method given by the Rack::Test like

post "#{url}.json",:data => [["Company","Website"],["this is the dummy text, with,comma","www.technology.com"],["some company","www.url.com"]]

But in my controller when check my params params[:data] i am receiving it as a single dimensioned array ["Company", "Website", "this is the dummy text, with comma", "www.technology.com", "some company", "www.url.com"]

But i want it to retain it's property as a multi-dimensional array. I need help to do this.

Xecutioner
  • 138
  • 5

3 Answers3

1

The Rack::Test methods all have the same signature... and the second param is a hash of params

i.e.

post '/path', params={}, rack_env={}

This is because they're just URL params - which are typical key/value structures (i.e. a hash) Why do you need it to be a multi-dimensional array?

EDIT: oh, I get it - you have a single hash with one key (:data)

If it's still causing you grief you could explicitly call to_param in there

ruby-1.9.2-p180 :003 > h = {:data => [["Company","Website"],["this is the dummy text, with,comma","www.technology.com"],["some company","www.url.com"]]}
 => {:data=>[["Company", "Website"], ["this is the dummy text, with,comma", "www.technology.com"], ["some company", "www.url.com"]]} 
ruby-1.9.2-p180 :004 > h.to_param
 => "data[][]=Company&data[][]=Website&data[][]=this+is+the+dummy+text%2C+with%2Ccomma&data[][]=www.technology.com&data[][]=some+company&data[][]=www.url.com" 
mylescarrick
  • 1,680
  • 8
  • 10
  • Thanks for your effort. Although it is sent as the key value pair like following format. `:data => [[],[],[]]` i want the value part to retain it's property as the multidimensional array is there any way to do that??. I desperately need it as an array to avoid another parsing method in my controller. – Xecutioner Jun 17 '11 at 06:01
  • It might be worth grabbing the source for Rack::Test and adding a further test - it looks like the 2d array is never tested for in @brynary's specs https://github.com/brynary/rack-test/blob/master/spec/rack/test/utils_spec.rb#L32 – mylescarrick Jun 17 '11 at 06:16
  • It is wierd that i get `"data"=>[nil, nil, nil, nil, nil, nil]` in the controller now when i passed it using h.to_params – Xecutioner Jun 17 '11 at 06:31
0

A workaround if you really need nested arrays is to change the request content type to JSON:

post url, JSON.dump([[1, 2], [3, 4]]), { "CONTENT_TYPE" => "application/json" }

This will correctly send a nested array through to the rack app.

0

Neither of the above worked too well for me but this did (see my original answer here).

The problem is body is sent as application/x-www-form-urlencoded by default, which doesn't handle multi-dimensional arrays too well. You can send it as application/json, but Sinatra probably won't merge the data into the request params. I use a middleware from rack-contrib which parses a json body from a POST request and merges it for you:

# Gemfile

`gem 'rack-contrib'`

then config.ru:

require 'rack/contrib'
require './app'

use Rack::PostBodyContentTypeParser
run Sinatra::Application

This won't be used in testing by default, but you can specify it:

# spec_helper.rb

OUTER_APP = Rack::Builder.parse_file("config.ru").first

module RSpecMixin
  include Rack::Test::Methods
  def app
    OUTER_APP # typically this might just be Sinatra::Application
  end
end

RSpec.configure do |config|
  config.include RSpecMixin
end

And example usage:

it 'is ok' do
  post '/', { key: 'value' }.to_json, { 'CONTENT_TYPE' => 'application/json' }
  expect(last_response).to be_ok
end
johansenja
  • 568
  • 1
  • 7
  • 18