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方法轉成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 轉成json放body
相關文章:
1. html - 移動端radio無法選中2. apache - 怎么給localhost后面默認加上8080端口3. css - 關于偽類背景問題4. python - 管道符和ssh傳文件5. c++ - win7在不刪除管理員密碼的前提下(密碼不為空),如何設置開機不需要密碼?6. mysql - 數(shù)據(jù)庫JOIN查詢7. windows-7 - Win7中Vmware Workstatoin與Xampp中Apache服務器端口沖突?8. 關于Navicat連接到mysql,我改了root的密碼后,Navicat連接報錯1862?9. php7.3.4中怎么開啟pdo驅動10. python - 用scrapy-splash爬取網(wǎng)站 為啥iframe下的內(nèi)容沒有被返回
