Ruby: How to turn a hash into HTTP parameters?

后端 未结 14 1579
故里飘歌
故里飘歌 2020-12-07 06:46

That is pretty easy with a plain hash like

{:a => \"a\", :b => \"b\"} 

which would translate into

\"a=a&b=b\"
<         


        
相关标签:
14条回答
  • 2020-12-07 07:53

    If you are in the context of a Faraday request, you can also just pass the params hash as the second argument and faraday takes care of making proper param URL part out of it:

    faraday_instance.get(url, params_hsh)
    
    0 讨论(0)
  • 2020-12-07 07:53

    I like using this gem:

    https://rubygems.org/gems/php_http_build_query

    Sample usage:

    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
    
    0 讨论(0)
提交回复
热议问题