ruby https post request模拟

    xiaoxiao2023-03-24  5

    ruby 1.9.0以上推荐使用gem插件  rest-client

    https://github.com/rest-client/rest-client

    代码样例:

          res = RestClient.post "https:", {params}, :content_type => :json, :accept => :json      resObj = JSON.parse(res) rescue {}

     其中有一个注意事项,作者的文档里没有做明确说明,当出现要传一个hash 数组 作为参数 的情况, 即如下的lis_attributes

          params = {

            token: xxxx,        a: {          lis_attributes: [ {b: 123, c: 234}]

    }      }  

    需要改成:

          params = {

            token: xxxx,        a: {          lis_attributes: { '': {b: 123, c: 234}}

    }      }  

    ruby1.8.7以及更低版本,推荐使用Ruby 自带 Net::HTTP控件

    样例代码:

        uri = URI("https://xxxxx/xxx")    toSend = {:supplier_id => id, :id => logistics_li.id}    https = Net::HTTP.new(uri.host,uri.port)    https.use_ssl = true    req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})    req['foo'] = 'bar'    req.set_form_data(toSend)    res = https.request(req)    raise "访问失败" unless res.body =~ /success/

    转载请注明原文地址: https://ju.6miu.com/read-1202651.html
    最新回复(0)