ruby - Sending array variables using HTTParty -


I'm sending a post request using HTTParty, one of the required variables is an array. This is the code I am using to send:

  feedback = HTTParty.post url ,: body = & gt; {"Key" = & gt; 'XYZ123', "content" = & gt; The API needs to be seen:  <{"placename" => "Placeholder", "Placeholder" = & gt;  

However, when I check the receipt of the request, <> "content": [{"placename": "placeholder", " Placecontent ":" sample content "}],

At the API, I think my code is producing:

 " content ": [ {"Placename": "placeholder"}, {"placecontent": "sample content"}],  

How can I prevent the array's records from being split into two?

Thank you.

Edit: The desired output of code is equivalent to

  ... and so on. Content [0] [placeen] = placeholder and content [0] [placecontent] = sample% 20 content ...  

By default, HTTParty uses a hash body to be converted to a parameter:

Example:

  {: name = & gt; "Bob" ,: Address = & gt; {: Alley = & gt; '111 Ruby Avenue' ,: City = & gt; 'Ruby Central' ,: Phone = & gt; ['111-111-1111', '222-222-2222']}} .to_params # = & gt; "Name = bob and address [city] = ruby ​​central and address [phone] [] = 111-111-1111 and address [phone] [] = 222-222-2222 and address [street] = 111  < / Pre> 

Ruby Avenue.

You can override this with your own converter using:

Override query string in normalized way: Normalization of useful array queries for overriding default amounts.

For a query:

  get '/' ,: query = & Gt; {: Sel Ected_ids = & gt; [1,2,3]}  

default query string generalized return:

  / select_ids [] = 1 & selected_ids [] = 2 & Let's change it:  
  / select_ids = 1 and select_ids = 2 and selected_ids = 3   

/ Pre>

Pass a process for query generalization that accepts a precipitated query.

@example Modify ARA query strings

  Class ServiceWrapper in HTTParty query_string_normalizer Proc {| Query | Query.map | Key, value | Value.map {| V | "# {Key} = # {v}"} end.join ('& amp;)}}  

@the ultimate [process] general customer string generalized @ island [hash, string] Query String @ Yildreon [array] An array that later '& amp;

or just pass it through your options:

  feedback = HTTParty.post url ,: body = & gt; {"Key" = & gt; 'XYZ123', "content" = & gt; [{"Plasmium" = & gt; "Placeholder", "Place Placecent" = & gt; "Sample content"}]} ,: query_string_normalizer = & gt; - & gt; (H) {... your own implementation is here ...}  

received a [1] = val1 and a [2] = val2 a [] = val1 and

  class based on MyHashConversions Def  a [] = val1 & amp; Based on a [] = val2  you can create your own  hashconversion  to_params (hash) parameters = hash.map {| Kashmir, V. Normalize_param (k, v)}. Go to Params.chop! # Back and amp; Parameter End Diff Normal Values ​​_param (key, value) param = '' stack = [] if value.is_a? (Array) #### Change is the ultimate one here & lt; & Lt; Value.each_with_index.map {| Element, i | Normalize_param ("# {key} [# {i}]", element)}. ### elsif value.is_a? (Hash) stack & lt; & Lt; [Key, value] and ultimate & lt; & Lt; "# {Key} = # {URI.encode (value.to_s, Regexp.new (" [^ # URI :: PATTERN :: UNRESERVED}] "))} & amp;" End Stack.Each | Parent, hash | Hash. Kashmir, V. If v.is_a? (Hash) stack & lt; & Lt; ["# {Parents} [# {k}]", v] The second ultimate & lt; & Lt; Normalize_param ("# {parent} [# {k}]", v) End End and Ultimate End End  

The above code is not checked, but if it works It is normal, and you can consider fixing the project and gitub, which gets fixed there, so it will work out of the box :)


Comments