httparty 0.24.1 → 0.24.3
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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +1 -1
- data/README.md +1 -1
- data/docs/README.md +30 -0
- data/examples/README.md +1 -0
- data/examples/multipart.rb +13 -0
- data/examples/tripit_sign_in.rb +2 -2
- data/lib/httparty/parser.rb +36 -5
- data/lib/httparty/request.rb +1 -1
- data/lib/httparty/response.rb +15 -1
- data/lib/httparty/version.rb +1 -1
- data/lib/httparty.rb +5 -4
- metadata +3 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2389baaffd615950f7d364a762d1c7e046e6e1819070b6974137a79737ed5cef
|
|
4
|
+
data.tar.gz: 33be08cb6b537efbbe5f691eff3308c9a126f03998d8cda1827ee946edadaffd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 205b2eda67b38f7d4febba842ee9fd13450ffae372f92455ed925b9646ad68872e6a60c7efe7ab97635209737eba19f1efddf8fd6ea58732b8552986304890f9
|
|
7
|
+
data.tar.gz: 431bf5342636a7a88bf3d64116535e20d547f089f6cabea8f66700215d9aaafc6ad8903e8205105183d24bb55115d676b28e6026d9c1a0aa969a74d6e53ec471
|
data/.github/workflows/ci.yml
CHANGED
data/README.md
CHANGED
|
@@ -19,7 +19,7 @@ gem install httparty
|
|
|
19
19
|
|
|
20
20
|
```ruby
|
|
21
21
|
# Use the class methods to get down to business quickly
|
|
22
|
-
response = HTTParty.get('
|
|
22
|
+
response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
|
|
23
23
|
|
|
24
24
|
puts response.body, response.code, response.message, response.headers.inspect
|
|
25
25
|
|
data/docs/README.md
CHANGED
|
@@ -4,6 +4,7 @@ Makes http fun again!
|
|
|
4
4
|
|
|
5
5
|
## Table of contents
|
|
6
6
|
- [Parsing JSON](#parsing-json)
|
|
7
|
+
- [File Uploads (Multipart)](#file-uploads-multipart)
|
|
7
8
|
- [Working with SSL](#working-with-ssl)
|
|
8
9
|
|
|
9
10
|
## Parsing JSON
|
|
@@ -28,6 +29,35 @@ HTTParty.post('http://example.com', body: JSON.generate({ foo: 'bar' }), headers
|
|
|
28
29
|
HTTParty.post('http://example.com', body: { foo: 'bar' }.to_json, headers: { 'Content-Type' => 'application/json' })
|
|
29
30
|
```
|
|
30
31
|
|
|
32
|
+
## File Uploads (Multipart)
|
|
33
|
+
|
|
34
|
+
When you include a `File` object in the body, HTTParty automatically uses `multipart/form-data` encoding:
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
HTTParty.post('http://example.com/upload',
|
|
38
|
+
body: {
|
|
39
|
+
name: 'Foo Bar',
|
|
40
|
+
avatar: File.open('/path/to/avatar.jpg')
|
|
41
|
+
}
|
|
42
|
+
)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Streaming Uploads for Large Files
|
|
46
|
+
|
|
47
|
+
For large file uploads, you can enable streaming mode to reduce memory usage. Instead of loading the entire file into memory, HTTParty will stream the file in chunks:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
HTTParty.post('http://example.com/upload',
|
|
51
|
+
body: {
|
|
52
|
+
name: 'Foo Bar',
|
|
53
|
+
avatar: File.open('/path/to/large_file.zip')
|
|
54
|
+
},
|
|
55
|
+
stream_body: true
|
|
56
|
+
)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**Note:** Some servers may not handle streaming uploads correctly. If you encounter issues (e.g., 400 errors), try without the `stream_body` option.
|
|
60
|
+
|
|
31
61
|
## Working with SSL
|
|
32
62
|
|
|
33
63
|
You can use this guide to work with SSL certificates.
|
data/examples/README.md
CHANGED
data/examples/multipart.rb
CHANGED
|
@@ -20,3 +20,16 @@ HTTParty.post(
|
|
|
20
20
|
email: 'example@email.com'
|
|
21
21
|
}
|
|
22
22
|
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# For large file uploads, use stream_body: true to reduce memory usage.
|
|
26
|
+
# Instead of loading the entire file into memory, HTTParty will stream it in chunks.
|
|
27
|
+
# Note: Some servers may not handle streaming uploads correctly.
|
|
28
|
+
|
|
29
|
+
HTTParty.post(
|
|
30
|
+
'http://localhost:3000/upload',
|
|
31
|
+
body: {
|
|
32
|
+
document: File.open('/full/path/to/large_file.zip')
|
|
33
|
+
},
|
|
34
|
+
stream_body: true
|
|
35
|
+
)
|
data/examples/tripit_sign_in.rb
CHANGED
|
@@ -9,7 +9,7 @@ class TripIt
|
|
|
9
9
|
def initialize(email, password)
|
|
10
10
|
@email = email
|
|
11
11
|
get_response = self.class.get('/account/login')
|
|
12
|
-
get_response_cookie = parse_cookie(get_response
|
|
12
|
+
get_response_cookie = parse_cookie(get_response)
|
|
13
13
|
|
|
14
14
|
post_response = self.class.post(
|
|
15
15
|
'/account/login',
|
|
@@ -20,7 +20,7 @@ class TripIt
|
|
|
20
20
|
headers: {'Cookie' => get_response_cookie.to_cookie_string }
|
|
21
21
|
)
|
|
22
22
|
|
|
23
|
-
@cookie = parse_cookie(post_response
|
|
23
|
+
@cookie = parse_cookie(post_response)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def account_settings
|
data/lib/httparty/parser.rb
CHANGED
|
@@ -103,11 +103,12 @@ module HTTParty
|
|
|
103
103
|
# @return [nil] when the response body is nil, an empty string, spaces only or "null"
|
|
104
104
|
def parse
|
|
105
105
|
return nil if body.nil?
|
|
106
|
+
# The bom has to be removed before the blank body checks below, otherwise a
|
|
107
|
+
# body that is nothing but a bom is handed to the format parser as an
|
|
108
|
+
# empty string and blows up instead of being reported as nil.
|
|
109
|
+
@body = strip_utf8_bom(body)
|
|
106
110
|
return nil if body == 'null'
|
|
107
111
|
return nil if body.valid_encoding? && body.strip.empty?
|
|
108
|
-
if body.valid_encoding? && body.encoding == Encoding::UTF_8
|
|
109
|
-
@body = body.gsub(/\A#{UTF8_BOM}/, '')
|
|
110
|
-
end
|
|
111
112
|
if supports_format?
|
|
112
113
|
parse_supported_format
|
|
113
114
|
else
|
|
@@ -119,14 +120,44 @@ module HTTParty
|
|
|
119
120
|
|
|
120
121
|
def xml
|
|
121
122
|
require 'multi_xml'
|
|
122
|
-
MultiXml.parse(body)
|
|
123
|
+
(defined?(MultiXML) ? MultiXML : MultiXml).parse(body)
|
|
123
124
|
end
|
|
124
125
|
|
|
125
126
|
UTF8_BOM = "\xEF\xBB\xBF"
|
|
127
|
+
UTF8_BOM_ENCODINGS = [Encoding::UTF_8, Encoding::US_ASCII, Encoding::BINARY].freeze
|
|
128
|
+
|
|
129
|
+
# Compares bytes rather than characters because Net::HTTP hands bodies
|
|
130
|
+
# over as ASCII-8BIT when the response has no charset, and a UTF-8 regex
|
|
131
|
+
# would never match those. ASCII-8BIT bodies in a format we don't parse
|
|
132
|
+
# are left alone, since they may be genuine binary data (images, files).
|
|
133
|
+
def strip_utf8_bom(string)
|
|
134
|
+
return string unless UTF8_BOM_ENCODINGS.include?(string.encoding)
|
|
135
|
+
return string if string.encoding == Encoding::BINARY && !supports_format?
|
|
136
|
+
return string.byteslice(UTF8_BOM.bytesize..-1) if utf8_bom?(string)
|
|
137
|
+
|
|
138
|
+
# Valid UTF-8 bodies have always been handed back as a copy, so mutating
|
|
139
|
+
# parsed_response doesn't also change response.body. Other encodings
|
|
140
|
+
# (ASCII-8BIT, US-ASCII, ISO-8859-1, invalid UTF-8) have always been
|
|
141
|
+
# handed back as the same object, and still are.
|
|
142
|
+
if string.encoding == Encoding::UTF_8 && string.valid_encoding?
|
|
143
|
+
string.dup
|
|
144
|
+
else
|
|
145
|
+
string
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Checks bytes one at a time so the common no-BOM case allocates nothing.
|
|
150
|
+
def utf8_bom?(string)
|
|
151
|
+
string.getbyte(0) == 0xEF && string.getbyte(1) == 0xBB && string.getbyte(2) == 0xBF
|
|
152
|
+
end
|
|
126
153
|
|
|
127
154
|
def json
|
|
128
155
|
require 'json'
|
|
129
|
-
JSON.
|
|
156
|
+
if JSON::VERSION.to_i >= 3
|
|
157
|
+
JSON.parse(body, :allow_nan => true)
|
|
158
|
+
else
|
|
159
|
+
JSON.parse(body, :quirks_mode => true, :allow_nan => true)
|
|
160
|
+
end
|
|
130
161
|
end
|
|
131
162
|
|
|
132
163
|
def csv
|
data/lib/httparty/request.rb
CHANGED
|
@@ -251,7 +251,7 @@ module HTTParty
|
|
|
251
251
|
@raw_request['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
252
252
|
end
|
|
253
253
|
|
|
254
|
-
if body.streaming? && options[:stream_body]
|
|
254
|
+
if body.streaming? && options[:stream_body] == true
|
|
255
255
|
stream = body.to_stream
|
|
256
256
|
@raw_request.body_stream = stream
|
|
257
257
|
@raw_request['Content-Length'] = stream.size.to_s
|
data/lib/httparty/response.rb
CHANGED
|
@@ -133,13 +133,27 @@ module HTTParty
|
|
|
133
133
|
end
|
|
134
134
|
|
|
135
135
|
def throw_exception
|
|
136
|
-
|
|
136
|
+
raise_on = @request.options[:raise_on]
|
|
137
|
+
return if raise_on.nil?
|
|
138
|
+
|
|
139
|
+
matchers = raise_on.is_a?(Range) ? [raise_on] : [*raise_on]
|
|
140
|
+
|
|
141
|
+
if matchers.any? { |matcher| code_matches?(matcher) }
|
|
137
142
|
::Kernel.raise ::HTTParty::ResponseError.new(@response), "Code #{code} - #{body}"
|
|
138
143
|
end
|
|
139
144
|
end
|
|
140
145
|
|
|
141
146
|
private
|
|
142
147
|
|
|
148
|
+
def code_matches?(matcher)
|
|
149
|
+
case matcher
|
|
150
|
+
when Range
|
|
151
|
+
matcher.cover?(code) || matcher.cover?(code.to_s)
|
|
152
|
+
else
|
|
153
|
+
/#{matcher}/.match?(code.to_s)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
143
157
|
def warn_about_nil_deprecation
|
|
144
158
|
trace_line = caller.reject { |line| line.include?('httparty') }.first
|
|
145
159
|
warning = "[DEPRECATION] HTTParty will no longer override `response#nil?`. " \
|
data/lib/httparty/version.rb
CHANGED
data/lib/httparty.rb
CHANGED
|
@@ -41,7 +41,7 @@ module HTTParty
|
|
|
41
41
|
# [:+local_host+:] Local address to bind to before connecting.
|
|
42
42
|
# [:+local_port+:] Local port to bind to before connecting.
|
|
43
43
|
# [:+body_stream+:] Allow streaming to a REST server to specify a body_stream.
|
|
44
|
-
# [:+stream_body+:]
|
|
44
|
+
# [:+stream_body+:] When downloading with a block, avoids accumulating the response in memory. When uploading files, streams the request body to reduce memory usage (opt-in).
|
|
45
45
|
# [:+multipart+:] Force content-type to be multipart
|
|
46
46
|
#
|
|
47
47
|
# There are also another set of options with names corresponding to various class methods. The methods in question are those that let you set a class-wide default, and the options override the defaults on a request-by-request basis. Those options are:
|
|
@@ -84,14 +84,15 @@ module HTTParty
|
|
|
84
84
|
default_options[:log_format] = format
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
# Raises HTTParty::ResponseError if response
|
|
87
|
+
# Raises HTTParty::ResponseError if the response status matches a code,
|
|
88
|
+
# range, or regular expression string.
|
|
88
89
|
#
|
|
89
90
|
# class Foo
|
|
90
91
|
# include HTTParty
|
|
91
|
-
# raise_on [404, 500, '
|
|
92
|
+
# raise_on [404, 500..599, '3[0-9]*']
|
|
92
93
|
# end
|
|
93
94
|
def raise_on(codes = [])
|
|
94
|
-
default_options[:raise_on] = *codes
|
|
95
|
+
default_options[:raise_on] = codes.is_a?(Range) ? [codes] : [*codes]
|
|
95
96
|
end
|
|
96
97
|
|
|
97
98
|
# Allows setting http proxy information to be used
|
metadata
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: httparty
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.24.
|
|
4
|
+
version: 0.24.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John Nunemaker
|
|
8
8
|
- Sandro Turriate
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: csv
|
|
@@ -148,8 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
148
147
|
- !ruby/object:Gem::Version
|
|
149
148
|
version: '0'
|
|
150
149
|
requirements: []
|
|
151
|
-
rubygems_version:
|
|
152
|
-
signing_key:
|
|
150
|
+
rubygems_version: 4.0.18
|
|
153
151
|
specification_version: 4
|
|
154
152
|
summary: Makes http fun! Also, makes consuming restful web services dead easy.
|
|
155
153
|
test_files: []
|