{"id":15509873,"url":"https://github.com/svyatov/http_wrapper","last_synced_at":"2025-04-24T04:41:40.667Z","repository":{"id":10508212,"uuid":"12694539","full_name":"svyatov/http_wrapper","owner":"svyatov","description":"A simple wrapper around standard Ruby Net::HTTP library","archived":false,"fork":false,"pushed_at":"2021-06-30T02:16:09.000Z","size":79,"stargazers_count":4,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-24T04:41:35.981Z","etag":null,"topics":["http-client","http-wrapper","ruby","utility-library"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/svyatov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-09-09T06:28:32.000Z","updated_at":"2022-04-26T19:01:46.000Z","dependencies_parsed_at":"2022-08-30T17:41:56.743Z","dependency_job_id":null,"html_url":"https://github.com/svyatov/http_wrapper","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fhttp_wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fhttp_wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fhttp_wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fhttp_wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/svyatov","download_url":"https://codeload.github.com/svyatov/http_wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250566442,"owners_count":21451228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http-client","http-wrapper","ruby","utility-library"],"created_at":"2024-10-02T09:44:37.671Z","updated_at":"2025-04-24T04:41:40.596Z","avatar_url":"https://github.com/svyatov.png","language":"Ruby","readme":"# http_wrapper\n\n[![Gem Version](https://badge.fury.io/rb/http_wrapper.svg)](https://badge.fury.io/rb/http_wrapper)\n[![Build Status](https://travis-ci.org/svyatov/http_wrapper.svg?branch=master)](https://travis-ci.org/svyatov/http_wrapper)\n[![Depfu](https://badges.depfu.com/badges/772e76ac2a71ed84291f452cd0e04b83/overview.svg)](https://depfu.com/github/svyatov/http_wrapper?project_id=6879)\n[![Maintainability](https://api.codeclimate.com/v1/badges/41f8e8c507907ea20e2b/maintainability)](https://codeclimate.com/github/svyatov/http_wrapper/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/41f8e8c507907ea20e2b/test_coverage)](https://codeclimate.com/github/svyatov/http_wrapper/test_coverage)\n\nA simple wrapper around standard Ruby Net::HTTP library.\n\nIf you need something more fully-featured you should use absolutely awesome [HTTP](https://github.com/httprb/http) gem. ([Why?](https://twin.github.io/httprb-is-great/))\n\n---\n\n## Installation\n\nAdd this line to your Gemfile:\n\n```ruby\ngem 'http_wrapper', '~\u003e 4.0'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it manually:\n\n    $ gem install http_wrapper\n\nAnd require it in you code:\n\n```ruby\nrequire 'http_wrapper'\n```\n\n## Usage\n\nCreate wrapper object:\n\n```ruby\nhttp = HTTPWrapper.new\n```\n\n### Access unprotected resource located at **some_url**:\n\n```ruby\nresponse = http.get some_url\n\n# response is always an instance of Net::HTTPResponse\n```\n\nResource is redirecting? No problem! `http_wrapper` follows up to 10 sequential redirects by default.\nBut you can specify your own limits.\n\n```ruby\nhttp.max_redirects = 5\nresponse = http.get some_url\n```\n\nUrl doesn't have scheme? `http_wrapper` prefixes url with `http://` if scheme is missing.\n\n```ruby\nhttp.get 'example.com' # will correctly request 'http://example.com'\n```\n\n### Access resource protected by form-based authentication:\n\n1. Post your credentials and get authentication cookie\n\n    ```ruby\n    # 'username' and 'password' fields are examples, it's just query parameters\n\n    # credentials as body params\n    cookie = http.post_and_get_cookie some_url, body: { username: 'iamjohn', password: '$uperS1kret' }\n    # - or - credentials as GET query params\n    cookie = http.post_and_get_cookie some_url, query: { username: 'iamjohn', password: '$uperS1kret' }\n    ```\n\n2. Get protected resource with provided cookie\n\n    ```ruby\n    response = http.get some_url, cookie: cookie\n    ```\n\n### Access resource protected by basic access authentication:\n\n```ruby\nresponse = http.get 'http://example.com', auth: { login: 'iamjohn', password: 'iamnotjohn' }\n# =\u003e http://iamjohn:iamnotjohn@example.com\n```\n\n### Access resource mimicing AJAX\n\nAdd special header or use special method:\n\n```ruby\nresponse = http.get_ajax some_url\n# - or -\nresponse = http.get some_url, headers: { x_requested_with: 'XMLHttpRequest' }\n# - or -\nresponse = http.get some_url, headers: { 'X-Requested-With' =\u003e 'XMLHttpRequest' }\n```\n\n### Access JSON resource\n\nSame as before :)\n\n```ruby\nresponse = http.get_json some_url\n# - or -\nresponse = http.get some_url, content_type: 'application/json; charset=UTF-8'\n# - or -\nresponse = http.get some_url, headers: { content_type: 'application/json; charset=UTF-8' }\n# - or -\nresponse = http.get some_url, headers: { 'Content-Type' =\u003e 'application/json; charset=UTF-8' }\n```\n\n### Access JSON resource mimicing AJAX\n\nJust use special method :) (which sets `X-Requested-With` and `Content-Type` headers for you)\n\n```ruby\nresponse = http.get_ajax_json some_url, some_params\n```\n\nDifficult to remember what goes after what: `get_ajax_json` or `get_json_ajax`?\n`http_wrapper` got you covered. They both work, use whatever variant you like better.\n\n```ruby\n# the same as above\nresponse = http.get_json_ajax some_url, some_params\n```\n\n### Provide additional query parameters\n\nDon't worry about escaping, `http_wrapper` got you covered here either.\n\n```ruby\nresponse = http.get 'http://www.google.com', query: { message: 'Hi! M\u0026Ms!', user: 'iamjohn' }\n# =\u003e http://www.google.com/?message=Hi!%20M%26Ms!\u0026user=iamjohn\n```\n\nDon't worry about parameters that already in URL, they'll be merged.\n\n```ruby\nresponse = http.get 'http://www.google.com/?q=test', query: { user: 'iamjohn' }\n# =\u003e http://www.google.com/?q=test\u0026user=iamjohn\n```\n\n### Files upload\n\nYou can easily upload any number of files with `multipart/form-data` content type.\n\n```ruby\nhttp = HTTPWrapper.new\nparams = {\n  multipart: [\n    # ['file input field name', 'File instance or string', { filename: 'itsfile.jpg', content_type: '...' }]\n    ['user_photo', File.read('user_photo.jpg'), { filename: 'photo.jpg' }],\n    # last element is optional\n    ['user_pic', File.open('user_pic.jpg')],\n    # you can also specify other parameters\n    ['user_name', 'john griffin']\n  ],\n  # or you can specify other parameters in body section\n  # it will be merged with multipart data\n  body: {\n    user_age: 25\n  }\n}\nresponse = http.post some_url, params\n```\n\n### Set timeout\n\nBy default timeout is set to 10 seconds.\n\n```ruby\nhttp.timeout = 5 # in seconds\n# - or - on instantiation\nhttp = HTTPWrapper.new timeout: 5\n```\n\n### Set logger\n\nIf you need to debug your requests, it's as simple as to say to `http_wrapper` where to output debug information.\n\n```ruby\nlogger = Logger.new '/path/to/log_file'\nhttp.logger = logger\n# - or -\nhttp = HTTPWrapper.new logger: $stdout\n# - to switch logger off -\nhttp.logger = nil\n```\n\n### Work over SSL\n\n`http_wrapper` works with SSL out of the box and by default verifying domain SSL certificate.\nBut you can easily turn verification off if needed.\n\n```ruby\nhttp.verify_cert = false\n# - or - on instantiation\nhttp = HTTPWrapper.new verify_cert: false\n```\n\n### POST, PUT and DELETE requests\n\nOn each `get` method there are `post`, `put` and `delete` methods. Examples:\n\n```ruby\nhttp.post some_url, body: { user: 'iamjohn', password: 'secret' }\n# - or -\nhttp.put some_url, body: { user: 'iamjohn', password: 'secret' }\n# - or -\nhttp.delete some_url, query: { user: 'iamjohn' }\n```\n\nDefault content type header for these requests is `application/x-www-form-urlencoded; charset=UTF-8`.\n\nSo for `get_ajax` there are `post_ajax`, `put_ajax` and `delete_ajax`.\n\nFor `get_soap` there are `post_soap`, `put_soap` and `delete_soap`.\n\nFor `get_json` there are `post_json`, `put_json` and `delete_json`.\n\nAnd for `get_ajax_json`, there are `post_ajax_json`, `put_ajax_json` and `delete_ajax_json`.\n\n### Change User Agent\n\n```ruby\nhttp = HTTWrapper.new user_agent: 'custom user agent'\n# - or -\nhttp.user_agent = 'custom user agent'\nhttp.get sample_url\n# - or -\nhttp.get sample_url, user_agent: 'custom user agent'\n# - or -\nhttp.get sample_url, headers: { user_agent: 'custom user agent' }\n# the last one always replaces other definitions\n```\n\n### Perform own custom Net::HTTP requests\n\n```ruby\nuri = URI 'http://example.com'\n\nrequest = Net::HTTP::Head.new uri\n\nhttp.execute request, uri\n```\n\n### Full params hash example\n\n```ruby\n{\n  # Request Headers\n  headers: {\n    'Content-Type' =\u003e 'text/html',\n    'X-Requested-With' =\u003e 'XMLHttpRequest',\n    'User-Agent' =\u003e 'Chrome v123',\n    # - or - use symbols\n    content_type: 'text/xml',\n    x_requested_with: 'XMLHttpRequest',\n    user_agent: 'Chrome v123'\n  },\n\n  # Query Parameters\n  query: {\n    user: 'iamjohn',\n    'user-stuff' =\u003e '123abc'\n  },\n\n  # Cookie\n  cookie: 'all cookies in one string',\n\n  # Basic authentication credentials\n  auth: {\n    login: 'iamjohn',\n    password: 'secret'\n  },\n\n  # Request body\n  body: 'as a string',\n  # - or -\n  body: {\n    as: 'a hash'\n  },\n\n  # Shortcut for User-Agent header (headers hash takes precedence)\n  user_agent: 'UserAgent v1.2.3',\n\n  # Shortcut for Content-Type header (headers hash takes precedence)\n  content_type: 'text/xml',\n\n  # multipart/form-data for file uploads\n  # the format of array of arrays is important here!\n  multipart: [\n    # you can use File object\n    ['file_input_name', File.open('somefile.ext')],\n    # - or - string and specify filename\n    ['file_input_name', File.read('somefile.ext'), { filename: 'readme.txt' }],\n    # - or - full format\n    ['file_input_name', 'some file content', { filename: 'readme.txt', content_type: 'text/text' }],\n    # - or - add other simple parameters\n    ['user_name', 'john smith']\n  ]\n}\n```\n\nDon't worry if you mistype root parameters key. `http_wrapper` checks root parameters keys and instantiation options keys.\nIf any unknown options or parameters found, it raises the `UnknownKeyError` exception.\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvyatov%2Fhttp_wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsvyatov%2Fhttp_wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvyatov%2Fhttp_wrapper/lists"}