httparty 0.15.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +18 -0
  3. data/.github/workflows/ci.yml +26 -0
  4. data/.gitignore +3 -0
  5. data/.rubocop_todo.yml +1 -1
  6. data/{History.md → Changelog.md} +137 -0
  7. data/Gemfile +8 -1
  8. data/Guardfile +3 -2
  9. data/README.md +6 -6
  10. data/docs/README.md +120 -38
  11. data/examples/README.md +28 -11
  12. data/examples/aaws.rb +6 -2
  13. data/examples/body_stream.rb +14 -0
  14. data/examples/custom_parsers.rb +4 -0
  15. data/examples/headers_and_user_agents.rb +7 -3
  16. data/examples/idn.rb +10 -0
  17. data/examples/logging.rb +3 -3
  18. data/examples/microsoft_graph.rb +52 -0
  19. data/examples/multipart.rb +22 -0
  20. data/examples/peer_cert.rb +9 -0
  21. data/examples/stream_download.rb +8 -2
  22. data/httparty.gemspec +7 -4
  23. data/lib/httparty/connection_adapter.rb +59 -16
  24. data/lib/httparty/cookie_hash.rb +10 -8
  25. data/lib/httparty/decompressor.rb +102 -0
  26. data/lib/httparty/exceptions.rb +4 -1
  27. data/lib/httparty/hash_conversions.rb +28 -12
  28. data/lib/httparty/headers_processor.rb +32 -0
  29. data/lib/httparty/logger/apache_formatter.rb +31 -6
  30. data/lib/httparty/logger/curl_formatter.rb +9 -7
  31. data/lib/httparty/logger/logger.rb +5 -1
  32. data/lib/httparty/logger/logstash_formatter.rb +61 -0
  33. data/lib/httparty/module_inheritable_attributes.rb +6 -4
  34. data/lib/httparty/net_digest_auth.rb +15 -15
  35. data/lib/httparty/parser.rb +25 -16
  36. data/lib/httparty/request/body.rb +105 -0
  37. data/lib/httparty/request/multipart_boundary.rb +13 -0
  38. data/lib/httparty/request.rb +96 -105
  39. data/lib/httparty/response/headers.rb +6 -2
  40. data/lib/httparty/response.rb +59 -8
  41. data/lib/httparty/response_fragment.rb +21 -0
  42. data/lib/httparty/text_encoder.rb +72 -0
  43. data/lib/httparty/utils.rb +13 -0
  44. data/lib/httparty/version.rb +3 -1
  45. data/lib/httparty.rb +70 -25
  46. data/website/css/common.css +1 -1
  47. metadata +38 -106
  48. data/.simplecov +0 -1
  49. data/.travis.yml +0 -8
  50. data/features/basic_authentication.feature +0 -20
  51. data/features/command_line.feature +0 -95
  52. data/features/deals_with_http_error_codes.feature +0 -26
  53. data/features/digest_authentication.feature +0 -30
  54. data/features/handles_compressed_responses.feature +0 -27
  55. data/features/handles_multiple_formats.feature +0 -57
  56. data/features/steps/env.rb +0 -27
  57. data/features/steps/httparty_response_steps.rb +0 -56
  58. data/features/steps/httparty_steps.rb +0 -43
  59. data/features/steps/mongrel_helper.rb +0 -127
  60. data/features/steps/remote_service_steps.rb +0 -92
  61. data/features/supports_read_timeout_option.feature +0 -13
  62. data/features/supports_redirection.feature +0 -22
  63. data/features/supports_timeout_option.feature +0 -13
  64. data/spec/fixtures/delicious.xml +0 -23
  65. data/spec/fixtures/empty.xml +0 -0
  66. data/spec/fixtures/google.html +0 -3
  67. data/spec/fixtures/ssl/generate.sh +0 -29
  68. data/spec/fixtures/ssl/generated/1fe462c2.0 +0 -1
  69. data/spec/fixtures/ssl/generated/bogushost.crt +0 -13
  70. data/spec/fixtures/ssl/generated/ca.crt +0 -16
  71. data/spec/fixtures/ssl/generated/ca.key +0 -15
  72. data/spec/fixtures/ssl/generated/selfsigned.crt +0 -14
  73. data/spec/fixtures/ssl/generated/server.crt +0 -13
  74. data/spec/fixtures/ssl/generated/server.key +0 -15
  75. data/spec/fixtures/ssl/openssl-exts.cnf +0 -9
  76. data/spec/fixtures/twitter.csv +0 -2
  77. data/spec/fixtures/twitter.json +0 -1
  78. data/spec/fixtures/twitter.xml +0 -403
  79. data/spec/fixtures/undefined_method_add_node_for_nil.xml +0 -2
  80. data/spec/httparty/connection_adapter_spec.rb +0 -495
  81. data/spec/httparty/cookie_hash_spec.rb +0 -100
  82. data/spec/httparty/exception_spec.rb +0 -45
  83. data/spec/httparty/hash_conversions_spec.rb +0 -49
  84. data/spec/httparty/logger/apache_formatter_spec.rb +0 -41
  85. data/spec/httparty/logger/curl_formatter_spec.rb +0 -119
  86. data/spec/httparty/logger/logger_spec.rb +0 -38
  87. data/spec/httparty/net_digest_auth_spec.rb +0 -268
  88. data/spec/httparty/parser_spec.rb +0 -178
  89. data/spec/httparty/request_spec.rb +0 -1244
  90. data/spec/httparty/response_spec.rb +0 -347
  91. data/spec/httparty/ssl_spec.rb +0 -74
  92. data/spec/httparty_spec.rb +0 -877
  93. data/spec/spec_helper.rb +0 -59
  94. data/spec/support/ssl_test_helper.rb +0 -47
  95. data/spec/support/ssl_test_server.rb +0 -80
  96. data/spec/support/stub_response.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 623e3c0f072b6dbd7ac990ec0eed857fb6639ea1
4
- data.tar.gz: 19d364f6fe90c6ab731df0900e22eab28ae8792f
2
+ SHA256:
3
+ metadata.gz: 9dccd9b1a1fa3f98ca96da312ccdf19371418815a556b1443acac322a8ae4e15
4
+ data.tar.gz: 61f75235d1dcf4e1e38cbab62869aeeff28de9d0dc27fad30c7ca11d6355cd0d
5
5
  SHA512:
6
- metadata.gz: 900978930b87670ca410e9ecc634dc1846e47b3d1da5abf4f599d598e70ea4d640b0aa03ed6cbfb66f88109d18b8f84da845720d124b0e944ad8ec37509004ec
7
- data.tar.gz: 5f7795609a87edf4b28f1b2a9db1b37d9d2be1a75d228e6e6545b9520b3225bbce00e27a0d8a1f0c558edd4f7daf8feb40cc7b6851ca17cccb8065b0589141c9
6
+ metadata.gz: e47fce0ce8b6c4fe97ee0335b4f0544bc039755f12f0c2aac9f47e901739fd172390a3dc01f070d6799b1af335293b5d09a66514deb0810dfafc37f25d2f3c81
7
+ data.tar.gz: 9aa37800410a58a60ca751de44eed1026514b26c408a1f890b48a8f0049c814052a70a55343d3798c3d4d59ca9b157257c1fa83240200d283ba374a9b62db2ef
data/.editorconfig ADDED
@@ -0,0 +1,18 @@
1
+ ; This file is for unifying the coding style for different editors and IDEs.
2
+ ; More information at http://EditorConfig.org
3
+
4
+ root = true
5
+ [*]
6
+ end_of_line = lf
7
+ trim_trailing_whitespace = true
8
+
9
+ [**.rb]
10
+ indent_size = 2
11
+ indent_style = spaces
12
+ insert_final_newline = true
13
+
14
+ [**.xml]
15
+ trim_trailing_whitespace = false
16
+
17
+ [**.html]
18
+ trim_trailing_whitespace = false
@@ -0,0 +1,26 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ build:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ ruby:
9
+ - 2.3
10
+ - 2.4
11
+ - 2.5
12
+ - 2.6
13
+ - 2.7
14
+ - '3.0' # Quoted, to avoid YAML float 3.0 interplated to "3"
15
+ - 3.1
16
+ - 3.2
17
+ steps:
18
+ - name: Check out repository code
19
+ uses: actions/checkout@v3
20
+ - name: Set up Ruby
21
+ uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby }}
24
+ bundler-cache: true # Run "bundle install", and cache the result automatically.
25
+ - name: Run Rake
26
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -9,3 +9,6 @@ pkg/
9
9
  /.bundle
10
10
  .rvmrc
11
11
  coverage
12
+ *.gem
13
+ .idea
14
+ .tool-versions
data/.rubocop_todo.yml CHANGED
@@ -15,7 +15,7 @@ Lint/EndAlignment:
15
15
  Enabled: false
16
16
 
17
17
  # Offense count: 1
18
- Lint/HandleExceptions:
18
+ Lint/SuppressedException:
19
19
  Enabled: false
20
20
 
21
21
  # Offense count: 5
@@ -1,3 +1,140 @@
1
+ ## 0.21.0
2
+
3
+ * [escape filename in the multipart/form-data Content-Disposition header](https://github.com/jnunemaker/httparty/commit/cdb45a678c43e44570b4e73f84b1abeb5ec22b8e)
4
+ * [Fix request marshaling](https://github.com/jnunemaker/httparty/pull/767)
5
+ * [Replace `mime-types` with `mini_mime`](https://github.com/jnunemaker/httparty/pull/769)
6
+
7
+ ## 0.20.0
8
+
9
+ Breaking changes
10
+
11
+ * Require Ruby >= 2.3.0
12
+
13
+ Fixes
14
+
15
+ * [`Marshal.dump` fails on response objects when request option `:logger` is set or `:parser` is a proc](https://github.com/jnunemaker/httparty/pull/714)
16
+ * [Switch `:pem` option to to `OpenSSL::PKey.read` to support other algorithms](https://github.com/jnunemaker/httparty/pull/720)
17
+
18
+ ## 0.19.1
19
+
20
+ * [Remove use of unary + method for creating non-frozen string to increase compatibility with older versions of ruby](https://github.com/jnunemaker/httparty/commit/4416141d37fd71bdba4f37589ec265f55aa446ce)
21
+
22
+ ## 0.19.0
23
+
24
+ * [Multipart/Form-Data: rewind files after read](https://github.com/jnunemaker/httparty/pull/709)
25
+ * [add frozen_string_literal pragma to all files](https://github.com/jnunemaker/httparty/pull/711)
26
+ * [Better handling of Accept-Encoding / Content-Encoding decompression (fixes #562)](https://github.com/jnunemaker/httparty/pull/729)
27
+
28
+ ## 0.18.1
29
+
30
+ * [Rename cop Lint/HandleExceptions to Lint/SuppressedException](https://github.com/jnunemaker/httparty/pull/699).
31
+ * [Encode keys in query params](https://github.com/jnunemaker/httparty/pull/698).
32
+ * [Fixed SSL doc example](https://github.com/jnunemaker/httparty/pull/692).
33
+ * [Add a build status badge](https://github.com/jnunemaker/httparty/pull/701).
34
+
35
+
36
+ ## 0.18.0
37
+
38
+ * [Support gzip/deflate transfer encoding when explicit headers are set](https://github.com/jnunemaker/httparty/pull/678).
39
+ * [Support edge case cookie format with a blank attribute](https://github.com/jnunemaker/httparty/pull/685).
40
+
41
+ ## 0.17.3
42
+
43
+ 0.17.2 is broken https://github.com/jnunemaker/httparty/issues/681
44
+
45
+ ## 0.17.2
46
+
47
+ * [Add Response#nil? deprecetion warning](https://github.com/jnunemaker/httparty/pull/680)
48
+
49
+ ## 0.17.1
50
+
51
+ * [Pass options to dynamic block headers](https://github.com/jnunemaker/httparty/pull/661)
52
+ * [Normalize urls with URI adapter to allow International Domain Names support](https://github.com/jnunemaker/httparty/pull/668)
53
+ * [Add max_retries support](https://github.com/jnunemaker/httparty/pull/660)
54
+ * [Minize gem size by removing test files](https://github.com/jnunemaker/httparty/pull/658)
55
+
56
+ ## 0.17.0
57
+
58
+ * [Fix encoding of streamed chunk](https://github.com/jnunemaker/httparty/pull/644)
59
+ * [Avoid modifying frozen strings](https://github.com/jnunemaker/httparty/pull/649)
60
+ * [Expose .connection on fragment block param](https://github.com/jnunemaker/httparty/pull/648)
61
+ * [Add support for `Net::HTTP#write_timeout` method (Ruby 2.6.0)](https://github.com/jnunemaker/httparty/pull/647)
62
+
63
+ ## 0.16.4
64
+ * [Add support for Ruby 2.6](https://github.com/jnunemaker/httparty/pull/636)
65
+ * [Fix a few multipart issues](https://github.com/jnunemaker/httparty/pull/626)
66
+ * [Improve a memory usage for https requests](https://github.com/jnunemaker/httparty/pull/625)
67
+ * [Add response code to streamed body](https://github.com/jnunemaker/httparty/pull/588)
68
+
69
+ ## 0.16.3
70
+ * [Add Logstash-compatible formatter](https://github.com/jnunemaker/httparty/pull/612)
71
+ * [Add support for headers specified with symbols](https://github.com/jnunemaker/httparty/pull/622)
72
+ * [Fix response object marshalling](https://github.com/jnunemaker/httparty/pull/618)
73
+ * [Add ability to send multipart, without passing file](https://github.com/jnunemaker/httparty/pull/615)
74
+ * [Fix detection of content_type for multipart payload](https://github.com/jnunemaker/httparty/pull/616)
75
+ * [Process dynamic headers before making actual request](https://github.com/jnunemaker/httparty/pull/606)
76
+ * [Fix multipart uploads with ActionDispatch::Http::UploadedFile TempFile by using original_filename](https://github.com/jnunemaker/httparty/pull/598)
77
+ * [Added support for lock and unlock http requests](https://github.com/jnunemaker/httparty/pull/596)
78
+
79
+ ## 0.16.2
80
+
81
+ * [Support ActionDispatch::Http::UploadedFile again](https://github.com/jnunemaker/httparty/pull/585)
82
+
83
+ ## 0.16.1
84
+
85
+ * [Parse content with application/hal+json content type as JSON](https://github.com/jnunemaker/httparty/pull/573)
86
+ * [Convert objects to string when concatenating in multipart stuff](https://github.com/jnunemaker/httparty/pull/575)
87
+ * [Fix multipart to set its header even when other headers are provided](https://github.com/jnunemaker/httparty/pull/576)
88
+
89
+ ## 0.16.0
90
+
91
+ * [Add multipart support](https://github.com/jnunemaker/httparty/pull/569)
92
+
93
+ ## 0.15.7
94
+
95
+ Fixed
96
+
97
+ * [Add Response#pretty_print | Restore documented behavior](https://github.com/jnunemaker/httparty/pull/570)
98
+ * [Add ability to parse response from JSONAPI ](https://github.com/jnunemaker/httparty/pull/553)
99
+
100
+ ## 0.15.6
101
+
102
+ Fixed
103
+
104
+ * [Encoding and content type stuff](https://github.com/jnunemaker/httparty/pull/543)
105
+
106
+ ## 0.15.5
107
+
108
+ Fixed
109
+
110
+ * [Use non-destructive gsub](https://github.com/jnunemaker/httparty/pull/540)
111
+
112
+ ## 0.15.4
113
+
114
+ Fixed
115
+
116
+ * Prevent gsub errors with different encodings.
117
+ * Prevent passing nil to encode_body.
118
+
119
+ ## 0.15.3
120
+
121
+ Fixed
122
+
123
+ * [Fix processing nil body for HEAD requests](https://github.com/jnunemaker/httparty/pull/530).
124
+ * Add missing require to headers.rb (33439a8).
125
+
126
+ ## 0.15.2
127
+
128
+ Fixed
129
+
130
+ * Remove symlink from specs. It was reportedly still getting bundled with gem.
131
+
132
+ ## 0.15.1
133
+
134
+ Fixed
135
+
136
+ * Stop including test files in gem. Fixes installation issues on windows due to symlink in spec dir.
137
+
1
138
  ## 0.15.0
2
139
 
3
140
  Breaking Changes
data/Gemfile CHANGED
@@ -2,8 +2,8 @@ source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
4
  gem 'rake'
5
- gem 'fakeweb', '~> 1.3'
6
5
  gem 'mongrel', '1.2.0.pre2'
6
+ gem 'json'
7
7
 
8
8
  group :development do
9
9
  gem 'guard'
@@ -12,8 +12,15 @@ group :development do
12
12
  end
13
13
 
14
14
  group :test do
15
+ gem 'rexml'
15
16
  gem 'rspec', '~> 3.4'
16
17
  gem 'simplecov', require: false
17
18
  gem 'aruba'
18
19
  gem 'cucumber', '~> 2.3'
20
+ gem 'webmock'
21
+ gem 'addressable'
22
+ end
23
+
24
+ group :development, :test do
25
+ gem 'pry'
19
26
  end
data/Guardfile CHANGED
@@ -1,7 +1,8 @@
1
1
  rspec_options = {
2
- version: 1,
3
2
  all_after_pass: false,
4
- all_on_start: false
3
+ all_on_start: false,
4
+ failed_mode: :keep,
5
+ cmd: 'bundle exec rspec',
5
6
  }
6
7
 
7
8
  guard 'rspec', rspec_options do
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # httparty
2
2
 
3
- Makes http fun again!
3
+ [![CI](https://github.com/jnunemaker/httparty/actions/workflows/ci.yml/badge.svg)](https://github.com/jnunemaker/httparty/actions/workflows/ci.yml)
4
+
5
+ Makes http fun again! Ain't no party like a httparty, because a httparty don't stop.
4
6
 
5
7
  ## Install
6
8
 
@@ -10,7 +12,7 @@ gem install httparty
10
12
 
11
13
  ## Requirements
12
14
 
13
- * Ruby 2.0.0 or higher
15
+ * Ruby 2.3.0 or higher
14
16
  * multi_xml
15
17
  * You like to party!
16
18
 
@@ -46,7 +48,6 @@ puts stack_exchange.users
46
48
  ```
47
49
 
48
50
  See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
49
-
50
51
  ## Command Line Interface
51
52
 
52
53
  httparty also includes the executable `httparty` which can be
@@ -63,9 +64,8 @@ httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
63
64
  ## Help and Docs
64
65
 
65
66
  * [Docs](https://github.com/jnunemaker/httparty/tree/master/docs)
66
- * https://groups.google.com/forum/#!forum/httparty-gem
67
- * http://rdoc.info/projects/jnunemaker/httparty
68
- * http://stackoverflow.com/questions/tagged/httparty
67
+ * https://github.com/jnunemaker/httparty/discussions
68
+ * https://www.rubydoc.info/github/jnunemaker/httparty
69
69
 
70
70
  ## Contributing
71
71
 
data/docs/README.md CHANGED
@@ -9,11 +9,25 @@ Makes http fun again!
9
9
  ## Parsing JSON
10
10
  If the response Content Type is `application/json`, HTTParty will parse the response and return Ruby objects such as a hash or array. The default behavior for parsing JSON will return keys as strings. This can be supressed with the `format` option. To get hash keys as symbols:
11
11
 
12
- ```
12
+ ```ruby
13
13
  response = HTTParty.get('http://example.com', format: :plain)
14
14
  JSON.parse response, symbolize_names: true
15
15
  ```
16
16
 
17
+ ## Posting JSON
18
+ When using Content Type `application/json` with `POST`, `PUT` or `PATCH` requests, the body should be a string of valid JSON:
19
+
20
+ ```ruby
21
+ # With written JSON
22
+ HTTParty.post('http://example.com', body: "{\"foo\":\"bar\"}", headers: { 'Content-Type' => 'application/json' })
23
+
24
+ # Using JSON.generate
25
+ HTTParty.post('http://example.com', body: JSON.generate({ foo: 'bar' }), headers: { 'Content-Type' => 'application/json' })
26
+
27
+ # Using object.to_json
28
+ HTTParty.post('http://example.com', body: { foo: 'bar' }.to_json, headers: { 'Content-Type' => 'application/json' })
29
+ ```
30
+
17
31
  ## Working with SSL
18
32
 
19
33
  You can use this guide to work with SSL certificates.
@@ -24,11 +38,10 @@ You can use this guide to work with SSL certificates.
24
38
  # Use this example if you are using a pem file
25
39
 
26
40
  class Client
27
- include HTTParty
28
-
29
- base_uri "https://example.com"
30
- pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
41
+ include HTTParty
31
42
 
43
+ base_uri "https://example.com"
44
+ pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
32
45
  end
33
46
  ```
34
47
 
@@ -38,11 +51,10 @@ end
38
51
  # Use this example if you are using a pkcs12 file
39
52
 
40
53
  class Client
41
- include HTTParty
42
-
43
- base_uri "https://example.com"
44
- pkcs12 File.read("#{File.expand_path('.')}/path/to/certs/cert.p12"), "123456"
54
+ include HTTParty
45
55
 
56
+ base_uri "https://example.com"
57
+ pkcs12 File.read("#{File.expand_path('.')}/path/to/certs/cert.p12"), "123456"
46
58
  end
47
59
  ```
48
60
 
@@ -52,11 +64,10 @@ end
52
64
  # Use this example if you are using a pkcs12 file
53
65
 
54
66
  class Client
55
- include HTTParty
56
-
57
- base_uri "https://example.com"
58
- ssl_ca_file "#{File.expand_path('.')}/path/to/certs/cert.pem"
67
+ include HTTParty
59
68
 
69
+ base_uri "https://example.com"
70
+ ssl_ca_file "#{File.expand_path('.')}/path/to/certs/cert.pem"
60
71
  end
61
72
  ```
62
73
 
@@ -66,44 +77,115 @@ end
66
77
  # Use this example if you are using a pkcs12 file
67
78
 
68
79
  class Client
69
- include HTTParty
70
-
71
- base_uri "https://example.com"
72
- ssl_ca_path '/path/to/certs'
80
+ include HTTParty
81
+
82
+ base_uri "https://example.com"
83
+ ssl_ca_path '/path/to/certs'
73
84
  end
74
85
  ```
75
86
 
76
- You can also include this options with the call:
87
+ You can also include all of these options with the call:
77
88
 
78
89
  ```ruby
79
90
  class Client
80
- include HTTParty
81
-
82
- base_uri "https://example.com"
83
-
84
- def self.fetch
85
- get("/resources", pem: (File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456")
86
- end
91
+ include HTTParty
92
+
93
+ base_uri "https://example.com"
94
+
95
+ def self.fetch
96
+ get("/resources", pem: File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), pem_password: "123456")
97
+ end
87
98
  end
88
99
  ```
89
100
 
90
101
  ### Avoid SSL verification
91
102
 
92
- In some cases you may want to skip SSL verification, because the entity that issue the certificate is not a valid one, but you still want to work with it. You can achieve this through:
103
+ In some cases you may want to skip SSL verification, because the entity that issued the certificate is not a valid one, but you still want to work with it. You can achieve this through:
93
104
 
94
105
  ```ruby
95
- #Skips SSL certificate verification
106
+ # Skips SSL certificate verification
96
107
 
97
108
  class Client
98
- include HTTParty
99
-
100
- base_uri "https://example.com"
101
- pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
102
-
103
- def self.fetch
104
- get("/resources", verify: false)
105
- # You can also use something like:
106
- # get("resources", verify_peer: false)
107
- end
109
+ include HTTParty
110
+
111
+ base_uri "https://example.com"
112
+ pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
113
+
114
+ def self.fetch
115
+ get("/resources", verify: false)
116
+ # You can also use something like:
117
+ # get("resources", verify_peer: false)
118
+ end
108
119
  end
109
- ```
120
+ ```
121
+
122
+ ### HTTP Compression
123
+
124
+ The `Accept-Encoding` request header and `Content-Encoding` response header
125
+ are used to control compression (gzip, etc.) over the wire. Refer to
126
+ [RFC-2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) for details.
127
+ (For clarity: these headers are **not** used for character encoding i.e. `utf-8`
128
+ which is specified in the `Accept` and `Content-Type` headers.)
129
+
130
+ Unless you have specific requirements otherwise, we recommend to **not** set
131
+ set the `Accept-Encoding` header on HTTParty requests. In this case, `Net::HTTP`
132
+ will set a sensible default compression scheme and automatically decompress the response.
133
+
134
+ If you explicitly set `Accept-Encoding`, there be dragons:
135
+
136
+ * If the HTTP response `Content-Encoding` received on the wire is `gzip` or `deflate`,
137
+ `Net::HTTP` will automatically decompress it, and will omit `Content-Encoding`
138
+ from your `HTTParty::Response` headers.
139
+
140
+ * For the following encodings, HTTParty will automatically decompress them if you include
141
+ the required gem into your project. Similar to above, if decompression succeeds,
142
+ `Content-Encoding` will be omitted from your `HTTParty::Response` headers.
143
+ **Warning:** Support for these encodings is experimental and not fully battle-tested.
144
+
145
+ | Content-Encoding | Required Gem |
146
+ | --- | --- |
147
+ | `br` (Brotli) | [brotli](https://rubygems.org/gems/brotli) |
148
+ | `compress` (LZW) | [ruby-lzws](https://rubygems.org/gems/ruby-lzws) |
149
+ | `zstd` (Zstandard) | [zstd-ruby](https://rubygems.org/gems/zstd-ruby) |
150
+
151
+ * For other encodings, `HTTParty::Response#body` will return the raw uncompressed byte string,
152
+ and you'll need to inspect the `Content-Encoding` response header and decompress it yourself.
153
+ In this case, `HTTParty::Response#parsed_response` will be `nil`.
154
+
155
+ * Lastly, you may use the `skip_decompression` option to disable all automatic decompression
156
+ and always get `HTTParty::Response#body` in its raw form along with the `Content-Encoding` header.
157
+
158
+ ```ruby
159
+ # Accept-Encoding=gzip,deflate can be safely assumed to be auto-decompressed
160
+
161
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'gzip,deflate,identity' })
162
+ JSON.parse(res.body) # safe
163
+
164
+
165
+ # Accept-Encoding=br,compress requires third-party gems
166
+
167
+ require 'brotli'
168
+ require 'lzws'
169
+ require 'zstd-ruby'
170
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'br,compress,zstd' })
171
+ JSON.parse(res.body)
172
+
173
+
174
+ # Accept-Encoding=* may return unhandled Content-Encoding
175
+
176
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => '*' })
177
+ encoding = res.headers['Content-Encoding']
178
+ if encoding
179
+ JSON.parse(your_decompression_handling(res.body, encoding))
180
+ else
181
+ # Content-Encoding not present implies decompressed
182
+ JSON.parse(res.body)
183
+ end
184
+
185
+
186
+ # Gimme the raw data!
187
+
188
+ res = HTTParty.get('https://example.com/test.json', skip_decompression: true)
189
+ encoding = res.headers['Content-Encoding']
190
+ JSON.parse(your_decompression_handling(res.body, encoding))
191
+ ```
data/examples/README.md CHANGED
@@ -13,22 +13,22 @@
13
13
  * Creates a custom parser for XML using crack gem
14
14
  * Uses `get` request
15
15
 
16
- * [Create HTML Nokogiri parser](nokogiri_html_parser.rb)
16
+ * [Create HTML Nokogiri parser](nokogiri_html_parser.rb)
17
17
  * Adds Html as a format
18
18
  * passed the body of request to Nokogiri
19
-
19
+
20
20
  * [More Custom Parsers](custom_parsers.rb)
21
21
  * Create an additional parser for atom or make it the ONLY parser
22
-
22
+
23
23
  * [Basic Auth, Delicious](delicious.rb)
24
24
  * Basic Auth, shows how to merge those into options
25
25
  * Uses `get` requests
26
-
26
+
27
27
  * [Passing Headers, User Agent](headers_and_user_agents.rb)
28
28
  * Use the class method of Httparty
29
29
  * Pass the User-Agent in the headers
30
30
  * Uses `get` requests
31
-
31
+
32
32
  * [Basic Post Request](basic.rb)
33
33
  * Httparty included into poro class
34
34
  * Uses `post` requests
@@ -36,7 +36,7 @@
36
36
  * [Access Rubyurl Shortener](rubyurl.rb)
37
37
  * Httparty included into poro class
38
38
  * Uses `post` requests
39
-
39
+
40
40
  * [Add a custom log file](logging.rb)
41
41
  * create a log file and have httparty log requests
42
42
 
@@ -44,23 +44,23 @@
44
44
  * Httparty included into poro class
45
45
  * Creates methods for different endpoints
46
46
  * Uses `get` requests
47
-
47
+
48
48
  * [Accessing Tripit](tripit_sign_in.rb)
49
49
  * Httparty included into poro class
50
50
  * Example of using `debug_output` to see headers/urls passed
51
51
  * Getting and using Cookies
52
52
  * Uses `get` requests
53
-
53
+
54
54
  * [Accessing Twitter](twitter.rb)
55
55
  * Httparty included into poro class
56
56
  * Basic Auth
57
- * Loads settings from a config file
57
+ * Loads settings from a config file
58
58
  * Uses `get` requests
59
59
  * Uses `post` requests
60
-
60
+
61
61
  * [Accessing WhoIsMyRep](whoismyrep.rb)
62
62
  * Httparty included into poro class
63
- * Uses `get` requests
63
+ * Uses `get` requests
64
64
  * Two ways to pass params to get, inline on the url or in query hash
65
65
 
66
66
  * [Rescue Json Error](rescue_json.rb)
@@ -70,3 +70,20 @@
70
70
  * Uses `get` requests
71
71
  * Uses `stream_body` mode
72
72
  * Download file without using the memory
73
+
74
+ * [Microsoft graph](microsoft_graph.rb)
75
+ * Basic Auth
76
+ * Uses `post` requests
77
+ * Uses multipart
78
+
79
+ * [Multipart](multipart.rb)
80
+ * Multipart data upload _(with and without file)_
81
+
82
+ * [Uploading File](body_stream.rb)
83
+ * Uses `body_stream` to upload file
84
+
85
+ * [Accessing x509 Peer Certificate](peer_cert.rb)
86
+ * Provides access to the server's TLS certificate
87
+
88
+ * [Accessing IDNs](idn.rb)
89
+ * Uses a `get` request with an International domain names, which are Urls with emojis and non-ASCII characters such as accented letters.
data/examples/aaws.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'active_support'
3
+ require 'active_support/core_ext/hash'
4
+ require 'active_support/core_ext/string'
3
5
 
4
6
  dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
7
  require File.join(dir, 'httparty')
@@ -13,14 +15,16 @@ module AAWS
13
15
  default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
14
16
 
15
17
  def initialize(key)
16
- self.class.default_params AWSAccessKeyId: key
18
+ @auth = { AWSAccessKeyId: key }
17
19
  end
18
20
 
19
21
  def search(options = {})
20
22
  raise ArgumentError, 'You must search for something' if options[:query].blank?
21
23
 
22
24
  # amazon uses nasty camelized query params
23
- options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
25
+ options[:query] = options[:query]
26
+ .reverse_merge(@auth)
27
+ .transform_keys { |k| k.to_s.camelize }
24
28
 
25
29
  # make a request and return the items (NOTE: this doesn't handle errors at this point)
26
30
  self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
@@ -0,0 +1,14 @@
1
+ # To upload file to a server use :body_stream
2
+
3
+ HTTParty.put(
4
+ 'http://localhost:3000/train',
5
+ body_stream: File.open('sample_configs/config_train_server_md.yml', 'r')
6
+ )
7
+
8
+
9
+ # Actually, it works with any IO object
10
+
11
+ HTTParty.put(
12
+ 'http://localhost:3000/train',
13
+ body_stream: StringIO.new('foo')
14
+ )
@@ -1,3 +1,7 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+
1
5
  class ParseAtom
2
6
  include HTTParty
3
7
 
@@ -1,6 +1,10 @@
1
1
  # To send custom user agents to identify your application to a web service (or mask as a specific browser for testing), send "User-Agent" as a hash to headers as shown below.
2
2
 
3
- require 'httparty'
3
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require File.join(dir, 'httparty')
5
+ require 'pp'
4
6
 
5
- APPLICATION_NAME = "Httparty"
6
- response = HTTParty.get('http://example.com', headers: {"User-Agent" => APPLICATION_NAME})
7
+ response = HTTParty.get('http://example.com', {
8
+ headers: {"User-Agent" => "Httparty"},
9
+ debug_output: STDOUT, # To show that User-Agent is Httparty
10
+ })
data/examples/idn.rb ADDED
@@ -0,0 +1,10 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+
5
+ class Idn
6
+ include HTTParty
7
+ uri_adapter Addressable::URI
8
+ end
9
+
10
+ pp Idn.get("https://i❤️.ws/emojidomain/💎?format=json")