httparty 0.13.0 → 0.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.editorconfig +18 -0
- data/.github/workflows/ci.yml +26 -0
- data/.gitignore +4 -0
- data/.rubocop.yml +92 -0
- data/.rubocop_todo.yml +124 -0
- data/CONTRIBUTING.md +23 -0
- data/Changelog.md +557 -0
- data/Gemfile +15 -3
- data/Guardfile +4 -3
- data/README.md +24 -25
- data/Rakefile +5 -7
- data/bin/httparty +20 -14
- data/docs/README.md +191 -0
- data/examples/README.md +89 -0
- data/examples/aaws.rb +10 -6
- data/examples/basic.rb +6 -10
- data/examples/body_stream.rb +14 -0
- data/examples/crack.rb +2 -2
- data/examples/custom_parsers.rb +6 -5
- data/examples/delicious.rb +8 -8
- data/examples/google.rb +2 -2
- data/examples/headers_and_user_agents.rb +7 -3
- data/examples/idn.rb +10 -0
- data/examples/logging.rb +36 -0
- data/examples/microsoft_graph.rb +52 -0
- data/examples/multipart.rb +22 -0
- data/examples/nokogiri_html_parser.rb +0 -3
- data/examples/peer_cert.rb +9 -0
- data/examples/rescue_json.rb +17 -0
- data/examples/rubyurl.rb +3 -3
- data/examples/stackexchange.rb +24 -0
- data/examples/stream_download.rb +26 -0
- data/examples/tripit_sign_in.rb +20 -9
- data/examples/twitter.rb +7 -7
- data/examples/whoismyrep.rb +1 -1
- data/httparty.gemspec +13 -9
- data/lib/httparty/connection_adapter.rb +105 -25
- data/lib/httparty/cookie_hash.rb +10 -9
- data/lib/httparty/decompressor.rb +102 -0
- data/lib/httparty/exceptions.rb +8 -2
- data/lib/httparty/hash_conversions.rb +39 -19
- data/lib/httparty/headers_processor.rb +32 -0
- data/lib/httparty/logger/apache_formatter.rb +47 -0
- data/lib/httparty/logger/curl_formatter.rb +93 -0
- data/lib/httparty/logger/logger.rb +22 -10
- data/lib/httparty/logger/logstash_formatter.rb +61 -0
- data/lib/httparty/module_inheritable_attributes.rb +6 -4
- data/lib/httparty/net_digest_auth.rb +76 -25
- data/lib/httparty/parser.rb +28 -15
- data/lib/httparty/request/body.rb +105 -0
- data/lib/httparty/request/multipart_boundary.rb +13 -0
- data/lib/httparty/request.rb +218 -130
- data/lib/httparty/response/headers.rb +23 -19
- data/lib/httparty/response.rb +99 -15
- data/lib/httparty/response_fragment.rb +21 -0
- data/lib/httparty/text_encoder.rb +72 -0
- data/lib/httparty/utils.rb +13 -0
- data/lib/httparty/version.rb +3 -1
- data/lib/httparty.rb +191 -83
- data/website/css/common.css +1 -1
- data/website/index.html +3 -3
- metadata +50 -120
- data/.travis.yml +0 -7
- data/History +0 -303
- data/features/basic_authentication.feature +0 -20
- data/features/command_line.feature +0 -7
- data/features/deals_with_http_error_codes.feature +0 -26
- data/features/digest_authentication.feature +0 -20
- data/features/handles_compressed_responses.feature +0 -27
- data/features/handles_multiple_formats.feature +0 -57
- data/features/steps/env.rb +0 -22
- data/features/steps/httparty_response_steps.rb +0 -52
- data/features/steps/httparty_steps.rb +0 -35
- data/features/steps/mongrel_helper.rb +0 -94
- data/features/steps/remote_service_steps.rb +0 -74
- data/features/supports_redirection.feature +0 -22
- data/features/supports_timeout_option.feature +0 -13
- data/lib/httparty/core_extensions.rb +0 -32
- data/lib/httparty/logger/apache_logger.rb +0 -22
- data/lib/httparty/logger/curl_logger.rb +0 -48
- data/spec/fixtures/delicious.xml +0 -23
- data/spec/fixtures/empty.xml +0 -0
- data/spec/fixtures/google.html +0 -3
- data/spec/fixtures/ssl/generate.sh +0 -29
- data/spec/fixtures/ssl/generated/1fe462c2.0 +0 -16
- data/spec/fixtures/ssl/generated/bogushost.crt +0 -13
- data/spec/fixtures/ssl/generated/ca.crt +0 -16
- data/spec/fixtures/ssl/generated/ca.key +0 -15
- data/spec/fixtures/ssl/generated/selfsigned.crt +0 -14
- data/spec/fixtures/ssl/generated/server.crt +0 -13
- data/spec/fixtures/ssl/generated/server.key +0 -15
- data/spec/fixtures/ssl/openssl-exts.cnf +0 -9
- data/spec/fixtures/twitter.csv +0 -2
- data/spec/fixtures/twitter.json +0 -1
- data/spec/fixtures/twitter.xml +0 -403
- data/spec/fixtures/undefined_method_add_node_for_nil.xml +0 -2
- data/spec/httparty/connection_adapter_spec.rb +0 -298
- data/spec/httparty/cookie_hash_spec.rb +0 -83
- data/spec/httparty/exception_spec.rb +0 -23
- data/spec/httparty/logger/apache_logger_spec.rb +0 -26
- data/spec/httparty/logger/curl_logger_spec.rb +0 -18
- data/spec/httparty/logger/logger_spec.rb +0 -22
- data/spec/httparty/net_digest_auth_spec.rb +0 -152
- data/spec/httparty/parser_spec.rb +0 -165
- data/spec/httparty/request_spec.rb +0 -631
- data/spec/httparty/response_spec.rb +0 -221
- data/spec/httparty/ssl_spec.rb +0 -74
- data/spec/httparty_spec.rb +0 -764
- data/spec/spec.opts +0 -2
- data/spec/spec_helper.rb +0 -37
- data/spec/support/ssl_test_helper.rb +0 -47
- data/spec/support/ssl_test_server.rb +0 -80
- data/spec/support/stub_response.rb +0 -43
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# httparty
|
2
2
|
|
3
|
-
|
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
|
15
|
+
* Ruby 2.3.0 or higher
|
14
16
|
* multi_xml
|
15
17
|
* You like to party!
|
16
18
|
|
@@ -18,41 +20,34 @@ gem install httparty
|
|
18
20
|
|
19
21
|
```ruby
|
20
22
|
# Use the class methods to get down to business quickly
|
21
|
-
response = HTTParty.get('http://
|
22
|
-
puts response.body, response.code, response.message, response.headers.inspect
|
23
|
+
response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow')
|
23
24
|
|
24
|
-
response.
|
25
|
-
puts item['user']['screen_name']
|
26
|
-
end
|
25
|
+
puts response.body, response.code, response.message, response.headers.inspect
|
27
26
|
|
28
27
|
# Or wrap things up in your own class
|
29
|
-
class
|
28
|
+
class StackExchange
|
30
29
|
include HTTParty
|
31
|
-
base_uri '
|
30
|
+
base_uri 'api.stackexchange.com'
|
32
31
|
|
33
|
-
def initialize(
|
34
|
-
@
|
32
|
+
def initialize(service, page)
|
33
|
+
@options = { query: { site: service, page: page } }
|
35
34
|
end
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
def timeline(which=:friends, options={})
|
40
|
-
options.merge!({:basic_auth => @auth})
|
41
|
-
self.class.get("/statuses/#{which}_timeline.json", options)
|
36
|
+
def questions
|
37
|
+
self.class.get("/2.2/questions", @options)
|
42
38
|
end
|
43
39
|
|
44
|
-
def
|
45
|
-
|
46
|
-
self.class.post('/statuses/update.json', options)
|
40
|
+
def users
|
41
|
+
self.class.get("/2.2/users", @options)
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
50
|
-
|
51
|
-
|
45
|
+
stack_exchange = StackExchange.new("stackoverflow", 1)
|
46
|
+
puts stack_exchange.questions
|
47
|
+
puts stack_exchange.users
|
52
48
|
```
|
53
49
|
|
54
50
|
See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
|
55
|
-
|
56
51
|
## Command Line Interface
|
57
52
|
|
58
53
|
httparty also includes the executable `httparty` which can be
|
@@ -63,18 +58,22 @@ formatted XML or JSON. Execute `httparty --help` for all the
|
|
63
58
|
options. Below is an example of how easy it is.
|
64
59
|
|
65
60
|
```
|
66
|
-
httparty "
|
61
|
+
httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
|
67
62
|
```
|
68
63
|
|
69
64
|
## Help and Docs
|
70
65
|
|
71
|
-
* https://
|
72
|
-
*
|
66
|
+
* [Docs](https://github.com/jnunemaker/httparty/tree/master/docs)
|
67
|
+
* https://github.com/jnunemaker/httparty/discussions
|
68
|
+
* https://www.rubydoc.info/github/jnunemaker/httparty
|
73
69
|
|
74
70
|
## Contributing
|
75
71
|
|
76
72
|
* Fork the project.
|
73
|
+
* Run `bundle`
|
74
|
+
* Run `bundle exec rake`
|
77
75
|
* Make your feature addition or bug fix.
|
78
76
|
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
77
|
+
* Run `bundle exec rake` (No, REALLY :))
|
79
78
|
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
|
80
79
|
* Send me a pull request. Bonus points for topic branches.
|
data/Rakefile
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
spec
|
4
|
-
|
5
|
-
spec.spec_files = FileList['spec/**/*_spec.rb']
|
6
|
-
spec.spec_opts = ['--options', 'spec/spec.opts']
|
1
|
+
begin
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
RSpec::Core::RakeTask.new(:spec)
|
4
|
+
rescue LoadError
|
7
5
|
end
|
8
6
|
|
9
7
|
require 'cucumber/rake/task'
|
10
8
|
Cucumber::Rake::Task.new(:features)
|
11
9
|
|
12
|
-
task :
|
10
|
+
task default: [:spec, :features]
|
data/bin/httparty
CHANGED
@@ -3,21 +3,21 @@
|
|
3
3
|
require "optparse"
|
4
4
|
require "pp"
|
5
5
|
|
6
|
-
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "/../lib"))
|
7
7
|
require "httparty"
|
8
8
|
|
9
9
|
opts = {
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
10
|
+
action: :get,
|
11
|
+
headers: {},
|
12
|
+
verbose: false
|
13
13
|
}
|
14
14
|
|
15
15
|
OptionParser.new do |o|
|
16
|
-
o.banner = "USAGE: #{$
|
16
|
+
o.banner = "USAGE: #{$PROGRAM_NAME} [options] [url]"
|
17
17
|
|
18
18
|
o.on("-f",
|
19
19
|
"--format [FORMAT]",
|
20
|
-
"Output format to use instead of pretty-print ruby: "
|
20
|
+
"Output format to use instead of pretty-print ruby: " \
|
21
21
|
"plain, csv, json or xml") do |f|
|
22
22
|
opts[:output_format] = f.downcase.to_sym
|
23
23
|
end
|
@@ -51,7 +51,7 @@ OptionParser.new do |o|
|
|
51
51
|
o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
|
52
52
|
abort "Invalid credentials format. Must be user:password" unless u =~ /.*:.+/
|
53
53
|
user, password = u.split(':')
|
54
|
-
opts[:basic_auth] = { :
|
54
|
+
opts[:basic_auth] = { username: user, password: password }
|
55
55
|
end
|
56
56
|
|
57
57
|
o.on("-r", "--response-code", "Command fails if response code >= 400") do
|
@@ -62,18 +62,22 @@ OptionParser.new do |o|
|
|
62
62
|
puts o
|
63
63
|
exit
|
64
64
|
end
|
65
|
-
end.parse!
|
66
65
|
|
66
|
+
o.on("--version", "Show HTTParty version") do |ver|
|
67
|
+
puts "Version: #{HTTParty::VERSION}"
|
68
|
+
exit
|
69
|
+
end
|
70
|
+
end.parse!
|
67
71
|
|
68
72
|
if ARGV.empty?
|
69
73
|
STDERR.puts "You need to provide a URL"
|
70
|
-
STDERR.puts "USAGE: #{$
|
74
|
+
STDERR.puts "USAGE: #{$PROGRAM_NAME} [options] [url]"
|
71
75
|
end
|
72
76
|
|
73
77
|
def dump_headers(response)
|
74
78
|
resp_type = Net::HTTPResponse::CODE_TO_OBJ[response.code.to_s]
|
75
79
|
puts "#{response.code} #{resp_type.to_s.sub(/^Net::HTTP/, '')}"
|
76
|
-
response.headers.each do |n,v|
|
80
|
+
response.headers.each do |n, v|
|
77
81
|
puts "#{n}: #{v}"
|
78
82
|
end
|
79
83
|
puts
|
@@ -81,7 +85,7 @@ end
|
|
81
85
|
|
82
86
|
if opts[:verbose]
|
83
87
|
puts "#{opts[:action].to_s.upcase} #{ARGV.first}"
|
84
|
-
opts[:headers].each do |n,v|
|
88
|
+
opts[:headers].each do |n, v|
|
85
89
|
puts "#{n}: #{v}"
|
86
90
|
end
|
87
91
|
puts
|
@@ -99,9 +103,11 @@ else
|
|
99
103
|
when :json
|
100
104
|
begin
|
101
105
|
require 'json'
|
102
|
-
puts JSON.pretty_generate(response.
|
106
|
+
puts JSON.pretty_generate(response.parsed_response)
|
103
107
|
rescue LoadError
|
104
|
-
puts YAML.dump(response
|
108
|
+
puts YAML.dump(response)
|
109
|
+
rescue JSON::JSONError
|
110
|
+
puts response.inspect
|
105
111
|
end
|
106
112
|
when :xml
|
107
113
|
require 'rexml/document'
|
@@ -109,7 +115,7 @@ else
|
|
109
115
|
puts
|
110
116
|
when :csv
|
111
117
|
require 'csv'
|
112
|
-
puts CSV.parse(response.body).map
|
118
|
+
puts CSV.parse(response.body).map(&:to_s)
|
113
119
|
else
|
114
120
|
puts response
|
115
121
|
end
|
data/docs/README.md
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
# httparty
|
2
|
+
|
3
|
+
Makes http fun again!
|
4
|
+
|
5
|
+
## Table of contents
|
6
|
+
- [Parsing JSON](#parsing-json)
|
7
|
+
- [Working with SSL](#working-with-ssl)
|
8
|
+
|
9
|
+
## Parsing JSON
|
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
|
+
|
12
|
+
```ruby
|
13
|
+
response = HTTParty.get('http://example.com', format: :plain)
|
14
|
+
JSON.parse response, symbolize_names: true
|
15
|
+
```
|
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
|
+
|
31
|
+
## Working with SSL
|
32
|
+
|
33
|
+
You can use this guide to work with SSL certificates.
|
34
|
+
|
35
|
+
#### Using `pem` option
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# Use this example if you are using a pem file
|
39
|
+
|
40
|
+
class Client
|
41
|
+
include HTTParty
|
42
|
+
|
43
|
+
base_uri "https://example.com"
|
44
|
+
pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
#### Using `pkcs12` option
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# Use this example if you are using a pkcs12 file
|
52
|
+
|
53
|
+
class Client
|
54
|
+
include HTTParty
|
55
|
+
|
56
|
+
base_uri "https://example.com"
|
57
|
+
pkcs12 File.read("#{File.expand_path('.')}/path/to/certs/cert.p12"), "123456"
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
#### Using `ssl_ca_file` option
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# Use this example if you are using a pkcs12 file
|
65
|
+
|
66
|
+
class Client
|
67
|
+
include HTTParty
|
68
|
+
|
69
|
+
base_uri "https://example.com"
|
70
|
+
ssl_ca_file "#{File.expand_path('.')}/path/to/certs/cert.pem"
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
#### Using `ssl_ca_path` option
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# Use this example if you are using a pkcs12 file
|
78
|
+
|
79
|
+
class Client
|
80
|
+
include HTTParty
|
81
|
+
|
82
|
+
base_uri "https://example.com"
|
83
|
+
ssl_ca_path '/path/to/certs'
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
You can also include all of these options with the call:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class Client
|
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
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
### Avoid SSL verification
|
102
|
+
|
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:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# Skips SSL certificate verification
|
107
|
+
|
108
|
+
class Client
|
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
|
119
|
+
end
|
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
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
## Examples
|
2
|
+
|
3
|
+
* [Amazon Book Search](aaws.rb)
|
4
|
+
* Httparty included into poro class
|
5
|
+
* Uses `get` requests
|
6
|
+
* Transforms query params to uppercased params
|
7
|
+
|
8
|
+
* [Google Search](google.rb)
|
9
|
+
* Httparty included into poro class
|
10
|
+
* Uses `get` requests
|
11
|
+
|
12
|
+
* [Crack Custom Parser](crack.rb)
|
13
|
+
* Creates a custom parser for XML using crack gem
|
14
|
+
* Uses `get` request
|
15
|
+
|
16
|
+
* [Create HTML Nokogiri parser](nokogiri_html_parser.rb)
|
17
|
+
* Adds Html as a format
|
18
|
+
* passed the body of request to Nokogiri
|
19
|
+
|
20
|
+
* [More Custom Parsers](custom_parsers.rb)
|
21
|
+
* Create an additional parser for atom or make it the ONLY parser
|
22
|
+
|
23
|
+
* [Basic Auth, Delicious](delicious.rb)
|
24
|
+
* Basic Auth, shows how to merge those into options
|
25
|
+
* Uses `get` requests
|
26
|
+
|
27
|
+
* [Passing Headers, User Agent](headers_and_user_agents.rb)
|
28
|
+
* Use the class method of Httparty
|
29
|
+
* Pass the User-Agent in the headers
|
30
|
+
* Uses `get` requests
|
31
|
+
|
32
|
+
* [Basic Post Request](basic.rb)
|
33
|
+
* Httparty included into poro class
|
34
|
+
* Uses `post` requests
|
35
|
+
|
36
|
+
* [Access Rubyurl Shortener](rubyurl.rb)
|
37
|
+
* Httparty included into poro class
|
38
|
+
* Uses `post` requests
|
39
|
+
|
40
|
+
* [Add a custom log file](logging.rb)
|
41
|
+
* create a log file and have httparty log requests
|
42
|
+
|
43
|
+
* [Accessing StackExchange](stackexchange.rb)
|
44
|
+
* Httparty included into poro class
|
45
|
+
* Creates methods for different endpoints
|
46
|
+
* Uses `get` requests
|
47
|
+
|
48
|
+
* [Accessing Tripit](tripit_sign_in.rb)
|
49
|
+
* Httparty included into poro class
|
50
|
+
* Example of using `debug_output` to see headers/urls passed
|
51
|
+
* Getting and using Cookies
|
52
|
+
* Uses `get` requests
|
53
|
+
|
54
|
+
* [Accessing Twitter](twitter.rb)
|
55
|
+
* Httparty included into poro class
|
56
|
+
* Basic Auth
|
57
|
+
* Loads settings from a config file
|
58
|
+
* Uses `get` requests
|
59
|
+
* Uses `post` requests
|
60
|
+
|
61
|
+
* [Accessing WhoIsMyRep](whoismyrep.rb)
|
62
|
+
* Httparty included into poro class
|
63
|
+
* Uses `get` requests
|
64
|
+
* Two ways to pass params to get, inline on the url or in query hash
|
65
|
+
|
66
|
+
* [Rescue Json Error](rescue_json.rb)
|
67
|
+
* Rescue errors due to parsing response
|
68
|
+
|
69
|
+
* [Download file using stream mode](stream_download.rb)
|
70
|
+
* Uses `get` requests
|
71
|
+
* Uses `stream_body` mode
|
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,26 +1,30 @@
|
|
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')
|
6
8
|
require 'pp'
|
7
|
-
config = YAML
|
9
|
+
config = YAML.load(File.read(File.join(ENV['HOME'], '.aaws')))
|
8
10
|
|
9
11
|
module AAWS
|
10
12
|
class Book
|
11
13
|
include HTTParty
|
12
14
|
base_uri 'http://ecs.amazonaws.com'
|
13
|
-
default_params :
|
15
|
+
default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
|
14
16
|
|
15
17
|
def initialize(key)
|
16
|
-
|
18
|
+
@auth = { AWSAccessKeyId: key }
|
17
19
|
end
|
18
20
|
|
19
|
-
def search(options={})
|
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]
|
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']
|
@@ -29,4 +33,4 @@ module AAWS
|
|
29
33
|
end
|
30
34
|
|
31
35
|
aaws = AAWS::Book.new(config[:access_key])
|
32
|
-
pp aaws.search(:
|
36
|
+
pp aaws.search(query: { title: 'Ruby On Rails' })
|
data/examples/basic.rb
CHANGED
@@ -3,13 +3,9 @@ require File.join(dir, 'httparty')
|
|
3
3
|
require 'pp'
|
4
4
|
|
5
5
|
# You can also use post, put, delete, head, options in the same fashion
|
6
|
-
response = HTTParty.get('
|
6
|
+
response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
|
7
7
|
puts response.body, response.code, response.message, response.headers.inspect
|
8
8
|
|
9
|
-
response.each do |item|
|
10
|
-
puts item['user']['screen_name']
|
11
|
-
end
|
12
|
-
|
13
9
|
# An example post to a minimal rails app in the development environment
|
14
10
|
# Note that "skip_before_filter :verify_authenticity_token" must be set in the
|
15
11
|
# "pears" controller for this example
|
@@ -20,11 +16,11 @@ class Partay
|
|
20
16
|
end
|
21
17
|
|
22
18
|
options = {
|
23
|
-
:
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
19
|
+
body: {
|
20
|
+
pear: { # your resource
|
21
|
+
foo: '123', # your columns/data
|
22
|
+
bar: 'second',
|
23
|
+
baz: 'last thing'
|
28
24
|
}
|
29
25
|
}
|
30
26
|
}
|
@@ -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
|
+
)
|
data/examples/crack.rb
CHANGED
@@ -9,11 +9,11 @@ class Rep
|
|
9
9
|
include HTTParty
|
10
10
|
|
11
11
|
parser(
|
12
|
-
|
12
|
+
proc do |body, format|
|
13
13
|
Crack::XML.parse(body)
|
14
14
|
end
|
15
15
|
)
|
16
16
|
end
|
17
17
|
|
18
18
|
pp Rep.get('http://whoismyrepresentative.com/getall_mems.php?zip=46544')
|
19
|
-
pp Rep.get('http://whoismyrepresentative.com/getall_mems.php', :
|
19
|
+
pp Rep.get('http://whoismyrepresentative.com/getall_mems.php', query: { zip: 46544 })
|
data/examples/custom_parsers.rb
CHANGED
@@ -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
|
|
@@ -16,13 +20,12 @@ class ParseAtom
|
|
16
20
|
parser Parser::Atom
|
17
21
|
end
|
18
22
|
|
19
|
-
|
20
23
|
class OnlyParseAtom
|
21
24
|
include HTTParty
|
22
25
|
|
23
26
|
# Only support Atom
|
24
27
|
class Parser::OnlyAtom < HTTParty::Parser
|
25
|
-
SupportedFormats = {"application/atom+xml" => :atom}
|
28
|
+
SupportedFormats = { "application/atom+xml" => :atom }
|
26
29
|
|
27
30
|
protected
|
28
31
|
|
@@ -35,7 +38,6 @@ class OnlyParseAtom
|
|
35
38
|
parser Parser::OnlyAtom
|
36
39
|
end
|
37
40
|
|
38
|
-
|
39
41
|
class SkipParsing
|
40
42
|
include HTTParty
|
41
43
|
|
@@ -49,11 +51,10 @@ class SkipParsing
|
|
49
51
|
parser Parser::Simple
|
50
52
|
end
|
51
53
|
|
52
|
-
|
53
54
|
class AdHocParsing
|
54
55
|
include HTTParty
|
55
56
|
parser(
|
56
|
-
|
57
|
+
proc do |body, format|
|
57
58
|
case format
|
58
59
|
when :json
|
59
60
|
body.to_json
|