ruby - Net::HTTP::POST 發(fā)送參數(shù)值為hash數(shù)組的方法
問題描述
代碼如下(很常見的發(fā)送post的方法):
def access_api(path, data)uri = URI(path)http = Net::HTTP.new(uri.host, uri.port)if uri.scheme == ’https’ http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.use_ssl = trueendbegin request = Net::HTTP::Post.new(uri.request_uri) request.set_form_data(data) res = http.request(request) if parsed[’code’] =1 parsed else nil endrescue puts ’communication failed’endend
這個方法發(fā)送類似{'name' => 'www.xxx.com', 'type'=>'download'}的參數(shù),沒什么問題,但是現(xiàn)在有一個需求參數(shù)里有一個數(shù)組,數(shù)組的元素是map,類似{'ip'=>{'static.xxx.com'=>80,'img.xxx.com'=>23}},這個該怎么搞
問題解答
回答1:可以使用Content-Type: application/json
body 放序列化的JSON
也可以使用to_query方法轉(zhuǎn)成url query string的形式
api: http://api.rubyonrails.org/classes/Object.html#method-i-to_query這是Rails里的方法

{:token=>'6df95c86c2be8f3d44eaa2da04f173ba', :name=>'www.xxxx.com', :type=>'download', :ip=>[{:'static.xxx.com'=>80}, {:'img.xxx.com'=>80}]}
to_json 轉(zhuǎn)成json放body
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. 如何設(shè)置一個無限循環(huán)并打破它。(Java線程)3. python小白 自學(xué)看書遇到看不懂的地方4. javascript - sublime快鍵鍵問題5. macos - 如何徹底刪除mac自帶的apache和php6. docker 17.03 怎么配置 registry mirror ?7. shell - mysql更新錯誤8. DADB.class.php文件的代碼怎么寫9. php對mysql提取數(shù)據(jù)那種速度更快10. 實(shí)現(xiàn)bing搜索工具urlAPI提交

網(wǎng)公網(wǎng)安備