ruby - Net::HTTP::POST 發(fā)送參數(shù)值為hash數(shù)組的方法
問(wèn)題描述
代碼如下(很常見(jiàn)的發(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
這個(gè)方法發(fā)送類(lèi)似{'name' => 'www.xxx.com', 'type'=>'download'}的參數(shù),沒(méi)什么問(wèn)題,但是現(xiàn)在有一個(gè)需求參數(shù)里有一個(gè)數(shù)組,數(shù)組的元素是map,類(lèi)似{'ip'=>{'static.xxx.com'=>80,'img.xxx.com'=>23}},這個(gè)該怎么搞
問(wèn)題解答
回答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. java - 阿里的開(kāi)發(fā)手冊(cè)中為什么禁用map來(lái)作為查詢的接受類(lèi)?2. android - 百度地圖加載完成監(jiān)聽(tīng)3. 關(guān)于docker下的nginx壓力測(cè)試4. nignx - docker內(nèi)nginx 80端口被占用5. docker images顯示的鏡像過(guò)多,狗眼被亮瞎了,怎么辦?6. dockerfile - [docker build image失敗- npm install]7. dockerfile - 我用docker build的時(shí)候出現(xiàn)下邊問(wèn)題 麻煩幫我看一下8. python3.x - git bash如何運(yùn)行.bat文件?9. golang - 用IDE看docker源碼時(shí)的小問(wèn)題10. docker api 開(kāi)發(fā)的端口怎么獲取?
