>  Q&A  >  본문

ruby - Net::HTTP::POST 发送参数值为hash数组的方法

代码如下(很常见的发送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 = true
end
begin
   request = Net::HTTP::Post.new(uri.request_uri)
   request.set_form_data(data)
   res = http.request(request)
   if parsed['code'] =1
      parsed
   else
      nil
   end
rescue
   puts 'communication failed'
end
end

这个方法发送类似{"name" => "www.xxx.com", "type"=>"download"}的参数,没什么问题,但是现在有一个需求参数里有一个数组,数组的元素是map,类似{"ip"=>{"static.xxx.com"=>80,"img.xxx.com"=>23}},这个该怎么搞

大家讲道理大家讲道理2710일 전858

모든 응답(1)나는 대답할 것이다

  • 黄舟

    黄舟2017-04-24 09:13:11

    사용 가능Content-Type: application/json

    본문에 직렬화된 JSON이 포함되어 있습니다

    to_query 메소드를 사용하여 URL 쿼리 문자열 형식으로 변환할 수도 있습니다

    api: http://api.rubyonrails.org/classes/Object.html#method-i-to_query
    Rails의 메소드입니다

    으아악

    to_json json으로 변환하고 본문을 넣습니다

    회신하다
    0
  • 취소회신하다