Search code examples
ruby-on-railsrubyhttprequesthttparty

post HTTP query via HTTParty


i want to use mailerlite API to add subscribers to a list

here is what APIdoc says to generate request

$subscriber = array(
    'email' => '[email protected]',
    'name' => 'foo',
    'fields' => array( 
       array( 'name' => 'country', 'value' => "usa" )
    )
);
$subscriber = $ML_Subscribers->setId( LIST_ID )->add( $subscriber );

and it generates a query string like this

email=foo%40bar.com&name=foo&fields%5B0%5D%5Bname%5D=country&fields%5B0%5D%5Bvalue%5D=usa

and sends it to mailerlite server my question is how to generate string via ruby HTTParty gem or rails methods


Solution

  • for generating exact same result as php code you should use this gem https://rubygems.org/gems/php_http_build_query

    example of use

    puts PHP.http_build_query({"a"=>"b","c"=>"d","e"=>[{"hello"=>"world","bah"=>"black"},{"hello"=>"world","bah"=>"black"}]})
    
    #a=b&c=d&e%5B0%5D%5Bbah%5D=black&e%5B0%5D%5Bhello%5D=world&e%5B1%5D%5Bbah%5D=black&e%5B1%5D%5Bhello%5D=world