• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby第三方模块unirest使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  • Creating Requests

    require 'unirest'
    response = Unirest.post 'http://httpbin.org/post',
                            headers:{ Accept:'application/json' },
                            parameters:{ age: 23, foo:'bar' }
    
    puts response.code # Status code
    puts response.headers # Response headers
    puts response.body # Parsed body
    puts response.raw_body # Unparsed body

     

  • File Uploads

    require 'unirest'
    Unirest.timeout(30)
    response = Unirest.post 'http://httpbin.org/post',
                            headers:{ Accept:'application/json' },
                            parameters:{ age:23, file:File.new(__dir__+'/test.text', 'rb') }
    puts response.body

     

  • Custom Entity Body

    require 'unirest'
    response = Unirest.post 'http://httpbin.org/post',
                            headers:{ Accept:'application/json' },
                            parameters:{ age:'value', foo:'bar' }.to_json # Converting the Hash to a JSON string
    puts response.body

     

  • Basic Authentication

    require 'unirest'
    response = Unirest.get 'http://httpbin.org/get', auth:{user:'username', password:'password'}
    puts response.body

     

  • Unirest.get(url, headers: {}, parameters: nil, auth:nil, &callback)
    Unirest.post(url, headers: {}, parameters: nil, auth:nil, &callback)
    Unirest.delete(url, headers: {}, parameters: nil, auth:nil, &callback)
    Unirest.put(url, headers: {}, parameters: nil, auth:nil, &callback)
    Unirest.patch(url, headers: {}, parameters: nil, auth:nil, &callback)
    • url (String) - Endpoint, address, or uri to be acted upon and requested information from.
    • headers (Object) - Request Headers as associative array or object
    • parameters (Array | Object | String) - Request Body associative array or object
    • callback (Function) - Optional; Asychronous callback method to be invoked upon result.

  • Response

    • code - HTTP Response Status Code (Example 200)
    • headers - HTTP Response Headers
    • body - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.
    • raw_body - Un-parsed response body

  • Advanced Configuration

    Timeout

    Unirest.timeout(5) # 5s timeout

    Default Request Headers

    Unirest.default_header('Header1','Value1')
    Unirest.default_header('Header2','Value2')
    Unirest.clear_default_headers()

    User-Agent

    Unirest.user_agent("custom_user_agent")

    参考:http://unirest.io/ruby.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
零零碎碎搞了一天最后发现是ruby版本问题发布时间:2022-07-14
下一篇:
Fedora16下安装rubyonrails发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap