nice_http 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bd8b790456a59571b66053164170f1e02cde243e279857078e4c26575355e5a
4
- data.tar.gz: f5e16724a65cdc6eb49317080a80bbac779baea4f13644cb036b43edd6521af9
3
+ metadata.gz: c9bc71e0ca49102f47bd703b2b8db0f50ee12c7187a18e9f5b546884a83dec72
4
+ data.tar.gz: 870f24566ae6cb3dad3c9cc5a0c05fc43817d6df5a5672c52b54f229e9809fb8
5
5
  SHA512:
6
- metadata.gz: a3362217bf1b2d3dc8e1d5a2b1d0055a70695cfbbf3be89b3002960e746d034372ae1783ff563e79547e7a9f419f8b1547acb3d50d6a2a28834504a9c1fd884d
7
- data.tar.gz: d2687b95760949160589ad9162be86e80a57fc15789b34dfd5e9dd6b52aeee6093b7cc88d6e1125040410a7f1d7f50379dfd1fcd3907267e1970b142d81afb35
6
+ metadata.gz: 0ebf9faa9628d042d296da43c649e67231ea90bc373e116e2381e17e69a92023d0148e2fa2b7d87c111b685d744f16e0c821145768b7553021e51d54a74101ab
7
+ data.tar.gz: 2f42aec22ce92847ff5d092f9606c2c3b7c47e3fb8bc722c009afad44387fa70d670ce1ce322ae75b46dddc86ad304033d01308d920c486d833b9c47bccd8d69
data/README.md CHANGED
@@ -1,366 +1,367 @@
1
- # NiceHttp
2
-
3
- [![Gem Version](https://badge.fury.io/rb/nice_http.svg)](https://rubygems.org/gems/nice_http)
4
- [![Build Status](https://travis-ci.com/MarioRuiz/nice_http.svg?branch=master)](https://github.com/MarioRuiz/nice_http)
5
- [![Coverage Status](https://coveralls.io/repos/github/MarioRuiz/nice_http/badge.svg?branch=master)](https://coveralls.io/github/MarioRuiz/nice_http?branch=master)
6
- [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http?ref=badge_shield)
7
-
8
- NiceHttp the simplest library for accessing and testing HTTP and REST resources.
9
-
10
- Manage different hosts on the fly. Easily get the value you want from the JSON strings. Use hashes on your requests.
11
-
12
- Also you can use mock responses by using :mock_response key on the request hash and enable the use_mocks option on NiceHttp.
13
-
14
- NiceHttp will take care of the redirections and the cookies, and for security tests you will be able to modify the cookies or disable and control the redirections by yourself.
15
-
16
- NiceHttp is able to use hashes as requests data and uses the Request Hash structure: https://github.com/MarioRuiz/Request-Hash
17
-
18
- **On the next link you have a full example using nice_http and RSpec to test REST APIs, Uber API and Reqres API: https://github.com/MarioRuiz/api-testing-example**
19
-
20
- To be able to generate random requests take a look at the documentation for nice_hash gem: https://github.com/MarioRuiz/nice_hash
21
-
22
- Example that creates 1000 good random and unique requests to register an user and test that the validation of the fields are correct by the user was able to be registered. Send 800 requests where just one field is wrong and verify the user was not able to be created: https://gist.github.com/MarioRuiz/824d7a462b62fd85f02c1a09455deefb
23
-
24
- ## Installation
25
-
26
- Install it yourself as:
27
-
28
- $ gem install nice_http
29
-
30
-
31
- ## A very simple first example
32
-
33
- ```ruby
34
- require 'nice_http'
35
-
36
- http = NiceHttp.new('https://reqres.in')
37
-
38
- resp = http.get("/api/users?page=2")
39
-
40
- pp resp.code
41
- pp resp.data.json
42
-
43
- resp = http.get("/api/users/2")
44
-
45
- pp resp.data.json(:first_name, :last_name)
46
-
47
- resp = http.post( {
48
- path: "/api/users",
49
- data: { name: "morpheus", job: "leader" }
50
- } )
51
-
52
- pp resp.data.json
53
- ```
54
-
55
- ## Create a connection
56
-
57
- The simplest way is just by supplying the value as an argument:
58
-
59
- ```ruby
60
-
61
- # as an url
62
- http1 = NiceHttp.new("https://example.com")
63
-
64
- # as parameters
65
- http2 = NiceHttp.new( host: "reqres.in", port: 443, ssl: true )
66
-
67
- # as a hash
68
- http3 = NiceHttp.new my_reqres_server
69
-
70
-
71
- ```
72
-
73
-
74
- You can specify all the defaults you will be using when creating connections by using the NiceHttp methods, in this example, http1 and http2 will be connecting to reqres.in with the default parameters and http3 to example.com:
75
-
76
- ```ruby
77
-
78
- # default parameters
79
- NiceHttp.host = 'reqres.in'
80
- NiceHttp.ssl = true
81
- NiceHttp.port = 443
82
- NiceHttp.debug = false
83
- NiceHttp.log = "./my_logs.log"
84
- NiceHttp.headers = {"api-key": "the api key"}
85
-
86
- http1 = NiceHttp.new()
87
-
88
- http2 = NiceHttp.new()
89
-
90
- http3 = NiceHttp.new("https://example.com")
91
-
92
- ```
93
-
94
- If you prefer to supply a hash to change the default settings for NiceHttp:
95
-
96
- ```ruby
97
- NiceHttp.defaults = {
98
- host: 'reqres.in',
99
- ssl: true,
100
- port: 443,
101
- debug: false,
102
- log: "./my_logs.log",
103
- headers: {"api-key": "the api key"}
104
- }
105
- ```
106
-
107
- To add a fixed path that would be added automatically to all your requests just before the specified request path, you can do it by adding it to `host`:
108
-
109
- ```ruby
110
- http = NiceHttp.new('https://v2.namsor.com/NamSorAPIv2/')
111
-
112
- resp = http.get('/api2/json/gender/Peter/Moon')
113
- # The get request path will be: /NamSorAPIv2/api2/json/gender/Peter/Moon on server v2.namsor.com
114
-
115
- resp = http.get('/api2/json/gender/Love/Sun?ret=true')
116
- # The get request path will be: /NamSorAPIv2/api2/json/gender/Love/Sun on server v2.namsor.com
117
- ```
118
-
119
- ## Creating requests
120
-
121
- You can use hash requests to simplify the management of your requests, for example creating a file specifying all the requests for your Customers API.
122
-
123
- The keys you can use:
124
-
125
- *path*: relative or absolute path, for example: "/api2/customers/update.do"
126
-
127
- *headers*: specific headers for the request. It will include a hash with the values.
128
-
129
- *data*: the data to be sent for example a JSON string. In case of supplying a Hash, Nice Http will assume that is a JSON and will convert it to a JSON string before sending the request and will add to the headers: 'Content-Type': 'application/json'
130
-
131
- *mock_response*: In case of use_mocks=true then NiceHttp will return this response
132
-
133
-
134
- Let's guess you have a file with this data for your requests on */requests/example.rb*:
135
-
136
- ```ruby
137
-
138
- module Requests
139
-
140
- module Example
141
-
142
- # simple get request example
143
- def self.list_of_users()
144
- {
145
- path: "/api/users?page=2"
146
- }
147
- end
148
-
149
- # post request example using a request hash that will be converted automatically to a json string
150
- def self.create_user_hash()
151
- {
152
- path: "/api/users",
153
- data: {
154
- name: "morpheus",
155
- job: "leader"
156
- }
157
- }
158
- end
159
-
160
- # post request example using a JSON string
161
- def self.create_user_raw()
162
- {
163
- path: "/api/users",
164
- headers: {"Content-Type": "application/json"},
165
- data: '{"name": "morpheus","job": "leader"}'
166
- }
167
- end
168
-
169
- end
170
-
171
- end
172
-
173
- ```
174
-
175
-
176
- Then in your code you can require this request file and use it like this:
177
-
178
- ```ruby
179
-
180
- resp = http.get Requests::Example.list_of_users
181
-
182
- pp resp.code
183
-
184
- resp = http.post Requests::Example.create_user_hash
185
-
186
- pp resp.data.json
187
-
188
-
189
- resp = http.post Requests::Example.create_user_raw
190
-
191
- pp resp.data.json(:job)
192
-
193
-
194
- ```
195
-
196
-
197
- In case you want to modify the request before sending it, for example just changing one field but the rest will be the same, you can supply a new key :values_for in the request hash that will contain a hash with the keys to be changed and NiceHttp will perform the necessary changes at any level:
198
-
199
- ```ruby
200
-
201
- req = Requests::Example.create_user_hash
202
- req.values_for = {job: "developer"}
203
-
204
- resp = http.post req
205
-
206
- pp resp.data.json
207
- #response: {:name=>"morpheus", :job=>"developer", :id=>"192", :createdAt=>"2018-12-14T14:41:54.371Z"}
208
-
209
- ```
210
-
211
- If the request hash contains a key :method with one of these possible values: :get, :head, :delete, :post or :patch, then it is possible to use the `send_request` method and pass just the request hash:
212
-
213
- ```ruby
214
- req= {
215
- path: "/api/users",
216
- method: :post,
217
- data: {
218
- name: "morpheus",
219
- job: "leader"
220
- }
221
- }
222
- resp = @http.send_request req
223
- ```
224
-
225
-
226
- ## Responses
227
-
228
- The response will include at least the keys:
229
-
230
- *code*: the http code response, for example: 200
231
-
232
- *message*: the http message response, for example: "OK"
233
-
234
- *data*: the data response structure. In case of json we can get it as a hash by using: `resp.data.json`. Also you can filter the json structure and get what you want: `resp.data.json(:loginname, :address)`
235
-
236
- Also interesting keys would be: *time_elapsed_total*, *time_elapsed* and many more available
237
-
238
-
239
- ## Special settings
240
-
241
- *debug*: (true or false) it will set the connecition on debug mode so you will be able to see the whole communication with the server in detail
242
-
243
- *log*: (:no, :screen, :file, :fix_file, "filename") it will log the basic communication for inspect. In case you want to add extra info to your logs you can do it for example adding to your code: http.logger.info "example extra log"
244
-
245
- *headers*: Hash containing the headers for the communication
246
-
247
- *cookies*: Hash containing the cookies for the communication
248
-
249
- *proxy_port, proxy_host*: in case you want to use a proxy for the connection
250
-
251
- *use_mocks*: (true or false) in case of true if the request hash contains a mock_response key it will be returning that response instead of trying to send the request.
252
-
253
- *auto_redirect*: (true or false) in case of true it will take care of the auto redirections.
254
-
255
- ## Authentication requests
256
-
257
- All we need to do is to add to our request the correct authentication tokens, seeds, headers.
258
-
259
- For example for Basic Authentication we need to add to the authorization header a seed generated with the user and password we want ot authenticate
260
-
261
- ```ruby
262
-
263
- @http = NiceHttp.new("https://jigsaw.w3.org/")
264
-
265
- @http.headers.authorization = NiceHttpUtils.basic_authentication(user: "guest", password: "guest")
266
-
267
- # headers will be in this example: {:authorization=>"Basic Z3Vlc3Q6Z3Vlc3Q=\n"}
268
-
269
- resp = @http.get("/HTTP/Basic/")
270
-
271
- ```
272
-
273
- Remember for other kind of authentication systems NiceHttp take care of the redirections and cookies that are requested to be set. In case you need to add a header with a token you can add it by using your NiceHttp object and the key headers, for example:
274
-
275
- ```ruby
276
- @http.headers.tokenuno = "xxx"
277
- # headers => {tokenuno: "xxx"}
278
-
279
- #another way:
280
- @http.headers[:tokendos] = "yyy"
281
- # headers => {tokenuno: "xxx", tokendos: "yyyy"}
282
-
283
- ```
284
-
285
- In case you want or need to control the redirections by yourself instead of allowing NiceHttp to do it, then set ```@http.auto_redirect = false```
286
-
287
- An example using OpenID authentication:
288
-
289
- ```ruby
290
- server = "https://samples.auth0.com/"
291
- path="/authorize?client_id=kbyuFDidLLm280LIwVFiazOqjO3ty8KH&response_type=code"
292
-
293
- @http = NiceHttp.new(server)
294
-
295
- resp = @http.get(path)
296
-
297
- p "With autoredirection:"
298
- p "Cookies: "
299
- p @http.cookies
300
- p "Code: #{resp.code} #{resp.message} "
301
- p "*"*40
302
-
303
- @http2 = NiceHttp.new(server)
304
- @http2.auto_redirect = false
305
-
306
- resp = @http2.get(path)
307
-
308
- p "Without autoredirection:"
309
- p "Cookies: "
310
- p @http2.cookies
311
- p "Code: #{resp.code} #{resp.message} "
312
-
313
- ```
314
-
315
- The output:
316
-
317
- ```
318
- "With autoredirection:"
319
- "Cookies: "
320
- {"/"=>{"auth0"=>"s%3A6vEEwmmIf-9YAG-NjvsOIyZAh-NS97jj.yFSXILdmCov6DRwXjEei3q3eHIrxZxHI4eg4%2BTpUaK4"},
321
- "/usernamepassword/login"=>{"_csrf"=>"bboZ0koMScwXkISzWaAMTYdY"}}
322
- "Code: 200 OK "
323
- "****************************************"
324
- "Without autoredirection:"
325
- "Cookies: "
326
- {"/"=>{"auth0"=>"s%3AcKndc44gllWyJv8FLztUIctuH4b__g0V.QEF3SOobK8%2FvX89iUKzGbfSP4Vt2bRtY2WH7ygBUkg4"}}
327
- "Code: 302 Found "
328
-
329
- ```
330
-
331
- You can see on the next link how to get the OAuth2 token for Microsoft Azure and add it to your Http connection header.
332
-
333
- https://gist.github.com/MarioRuiz/d3525185024737885c0c9afa6dc8b9e5
334
-
335
-
336
- ## Send multipart content
337
-
338
- Example posting a csv file:
339
-
340
- ```ruby
341
-
342
- require 'net/http/post/multipart'
343
- request = {
344
- path: "/customer/profile/",
345
- headers: {'Content-Type' => 'multipart/form-data'},
346
- data: (Net::HTTP::Post::Multipart.new "/customer/profile/",
347
- "file" => UploadIO.new("./path/to/my/file.csv", "text/csv"))
348
- }
349
- response=@http.post(request)
350
-
351
- ```
352
-
353
- ## Contributing
354
-
355
- Bug reports are very welcome on GitHub at https://github.com/marioruiz/nice_http.
356
-
357
- If you want to contribute please follow [GitHub Flow](https://guides.github.com/introduction/flow/index.html)
358
-
359
- ## License
360
-
361
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
362
-
363
-
364
-
365
-
1
+ # NiceHttp
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/nice_http.svg)](https://rubygems.org/gems/nice_http)
4
+ [![Build Status](https://travis-ci.com/MarioRuiz/nice_http.svg?branch=master)](https://github.com/MarioRuiz/nice_http)
5
+ [![Coverage Status](https://coveralls.io/repos/github/MarioRuiz/nice_http/badge.svg?branch=master)](https://coveralls.io/github/MarioRuiz/nice_http?branch=master)
6
+ [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http?ref=badge_shield)
7
+
8
+ NiceHttp the simplest library for accessing and testing HTTP and REST resources.
9
+
10
+ Manage different hosts on the fly. Easily get the value you want from the JSON strings. Use hashes on your requests.
11
+
12
+ Also you can use mock responses by using :mock_response key on the request hash and enable the use_mocks option on NiceHttp.
13
+
14
+ NiceHttp will take care of the redirections and the cookies, and for security tests you will be able to modify the cookies or disable and control the redirections by yourself.
15
+
16
+ NiceHttp is able to use hashes as requests data and uses the Request Hash structure: https://github.com/MarioRuiz/Request-Hash
17
+
18
+ **On the next link you have a full example using nice_http and RSpec to test REST APIs, Uber API and Reqres API: https://github.com/MarioRuiz/api-testing-example**
19
+
20
+ To be able to generate random requests take a look at the documentation for nice_hash gem: https://github.com/MarioRuiz/nice_hash
21
+
22
+ Example that creates 1000 good random and unique requests to register an user and test that the validation of the fields are correct by the user was able to be registered. Send 800 requests where just one field is wrong and verify the user was not able to be created: https://gist.github.com/MarioRuiz/824d7a462b62fd85f02c1a09455deefb
23
+
24
+ ## Installation
25
+
26
+ Install it yourself as:
27
+
28
+ $ gem install nice_http
29
+
30
+
31
+ ## A very simple first example
32
+
33
+ ```ruby
34
+ require 'nice_http'
35
+
36
+ http = NiceHttp.new('https://reqres.in')
37
+
38
+ resp = http.get("/api/users?page=2")
39
+
40
+ pp resp.code
41
+ pp resp.data.json
42
+
43
+ resp = http.get("/api/users/2")
44
+
45
+ pp resp.data.json(:first_name, :last_name)
46
+
47
+ resp = http.post( {
48
+ path: "/api/users",
49
+ data: { name: "morpheus", job: "leader" }
50
+ } )
51
+
52
+ pp resp.data.json
53
+ ```
54
+
55
+ ## Create a connection
56
+
57
+ The simplest way is just by supplying the value as an argument:
58
+
59
+ ```ruby
60
+
61
+ # as an url
62
+ http1 = NiceHttp.new("https://example.com")
63
+
64
+ # as parameters
65
+ http2 = NiceHttp.new( host: "reqres.in", port: 443, ssl: true )
66
+
67
+ # as a hash
68
+ http3 = NiceHttp.new my_reqres_server
69
+
70
+
71
+ ```
72
+
73
+
74
+ You can specify all the defaults you will be using when creating connections by using the NiceHttp methods, in this example, http1 and http2 will be connecting to reqres.in with the default parameters and http3 to example.com:
75
+
76
+ ```ruby
77
+
78
+ # default parameters
79
+ NiceHttp.host = 'reqres.in'
80
+ NiceHttp.ssl = true
81
+ NiceHttp.port = 443
82
+ NiceHttp.debug = false
83
+ NiceHttp.log = "./my_logs.log"
84
+ NiceHttp.headers = {"api-key": "the api key"}
85
+ NiceHttp.values_for = { region: 'europe', customerId: 334 }
86
+
87
+ http1 = NiceHttp.new()
88
+
89
+ http2 = NiceHttp.new()
90
+
91
+ http3 = NiceHttp.new("https://example.com")
92
+
93
+ ```
94
+
95
+ If you prefer to supply a hash to change the default settings for NiceHttp:
96
+
97
+ ```ruby
98
+ NiceHttp.defaults = {
99
+ host: 'reqres.in',
100
+ ssl: true,
101
+ port: 443,
102
+ debug: false,
103
+ log: "./my_logs.log",
104
+ headers: {"api-key": "the api key"}
105
+ }
106
+ ```
107
+
108
+ To add a fixed path that would be added automatically to all your requests just before the specified request path, you can do it by adding it to `host`:
109
+
110
+ ```ruby
111
+ http = NiceHttp.new('https://v2.namsor.com/NamSorAPIv2/')
112
+
113
+ resp = http.get('/api2/json/gender/Peter/Moon')
114
+ # The get request path will be: /NamSorAPIv2/api2/json/gender/Peter/Moon on server v2.namsor.com
115
+
116
+ resp = http.get('/api2/json/gender/Love/Sun?ret=true')
117
+ # The get request path will be: /NamSorAPIv2/api2/json/gender/Love/Sun on server v2.namsor.com
118
+ ```
119
+
120
+ ## Creating requests
121
+
122
+ You can use hash requests to simplify the management of your requests, for example creating a file specifying all the requests for your Customers API.
123
+
124
+ The keys you can use:
125
+
126
+ *path*: relative or absolute path, for example: "/api2/customers/update.do"
127
+
128
+ *headers*: specific headers for the request. It will include a hash with the values.
129
+
130
+ *data*: the data to be sent for example a JSON string. In case of supplying a Hash, Nice Http will assume that is a JSON and will convert it to a JSON string before sending the request and will add to the headers: 'Content-Type': 'application/json'
131
+
132
+ *mock_response*: In case of use_mocks=true then NiceHttp will return this response
133
+
134
+
135
+ Let's guess you have a file with this data for your requests on */requests/example.rb*:
136
+
137
+ ```ruby
138
+
139
+ module Requests
140
+
141
+ module Example
142
+
143
+ # simple get request example
144
+ def self.list_of_users()
145
+ {
146
+ path: "/api/users?page=2"
147
+ }
148
+ end
149
+
150
+ # post request example using a request hash that will be converted automatically to a json string
151
+ def self.create_user_hash()
152
+ {
153
+ path: "/api/users",
154
+ data: {
155
+ name: "morpheus",
156
+ job: "leader"
157
+ }
158
+ }
159
+ end
160
+
161
+ # post request example using a JSON string
162
+ def self.create_user_raw()
163
+ {
164
+ path: "/api/users",
165
+ headers: {"Content-Type": "application/json"},
166
+ data: '{"name": "morpheus","job": "leader"}'
167
+ }
168
+ end
169
+
170
+ end
171
+
172
+ end
173
+
174
+ ```
175
+
176
+
177
+ Then in your code you can require this request file and use it like this:
178
+
179
+ ```ruby
180
+
181
+ resp = http.get Requests::Example.list_of_users
182
+
183
+ pp resp.code
184
+
185
+ resp = http.post Requests::Example.create_user_hash
186
+
187
+ pp resp.data.json
188
+
189
+
190
+ resp = http.post Requests::Example.create_user_raw
191
+
192
+ pp resp.data.json(:job)
193
+
194
+
195
+ ```
196
+
197
+
198
+ In case you want to modify the request before sending it, for example just changing one field but the rest will be the same, you can supply a new key :values_for in the request hash that will contain a hash with the keys to be changed and NiceHttp will perform the necessary changes at any level:
199
+
200
+ ```ruby
201
+
202
+ req = Requests::Example.create_user_hash
203
+ req.values_for = {job: "developer"}
204
+
205
+ resp = http.post req
206
+
207
+ pp resp.data.json
208
+ #response: {:name=>"morpheus", :job=>"developer", :id=>"192", :createdAt=>"2018-12-14T14:41:54.371Z"}
209
+
210
+ ```
211
+
212
+ If the request hash contains a key :method with one of these possible values: :get, :head, :delete, :post or :patch, then it is possible to use the `send_request` method and pass just the request hash:
213
+
214
+ ```ruby
215
+ req= {
216
+ path: "/api/users",
217
+ method: :post,
218
+ data: {
219
+ name: "morpheus",
220
+ job: "leader"
221
+ }
222
+ }
223
+ resp = @http.send_request req
224
+ ```
225
+
226
+
227
+ ## Responses
228
+
229
+ The response will include at least the keys:
230
+
231
+ *code*: the http code response, for example: 200
232
+
233
+ *message*: the http message response, for example: "OK"
234
+
235
+ *data*: the data response structure. In case of json we can get it as a hash by using: `resp.data.json`. Also you can filter the json structure and get what you want: `resp.data.json(:loginname, :address)`
236
+
237
+ Also interesting keys would be: *time_elapsed_total*, *time_elapsed* and many more available
238
+
239
+
240
+ ## Special settings
241
+
242
+ *debug*: (true or false) it will set the connecition on debug mode so you will be able to see the whole communication with the server in detail
243
+
244
+ *log*: (:no, :screen, :file, :fix_file, "filename") it will log the basic communication for inspect. In case you want to add extra info to your logs you can do it for example adding to your code: http.logger.info "example extra log"
245
+
246
+ *headers*: Hash containing the headers for the communication
247
+
248
+ *cookies*: Hash containing the cookies for the communication
249
+
250
+ *proxy_port, proxy_host*: in case you want to use a proxy for the connection
251
+
252
+ *use_mocks*: (true or false) in case of true if the request hash contains a mock_response key it will be returning that response instead of trying to send the request.
253
+
254
+ *auto_redirect*: (true or false) in case of true it will take care of the auto redirections.
255
+
256
+ ## Authentication requests
257
+
258
+ All we need to do is to add to our request the correct authentication tokens, seeds, headers.
259
+
260
+ For example for Basic Authentication we need to add to the authorization header a seed generated with the user and password we want ot authenticate
261
+
262
+ ```ruby
263
+
264
+ @http = NiceHttp.new("https://jigsaw.w3.org/")
265
+
266
+ @http.headers.authorization = NiceHttpUtils.basic_authentication(user: "guest", password: "guest")
267
+
268
+ # headers will be in this example: {:authorization=>"Basic Z3Vlc3Q6Z3Vlc3Q=\n"}
269
+
270
+ resp = @http.get("/HTTP/Basic/")
271
+
272
+ ```
273
+
274
+ Remember for other kind of authentication systems NiceHttp take care of the redirections and cookies that are requested to be set. In case you need to add a header with a token you can add it by using your NiceHttp object and the key headers, for example:
275
+
276
+ ```ruby
277
+ @http.headers.tokenuno = "xxx"
278
+ # headers => {tokenuno: "xxx"}
279
+
280
+ #another way:
281
+ @http.headers[:tokendos] = "yyy"
282
+ # headers => {tokenuno: "xxx", tokendos: "yyyy"}
283
+
284
+ ```
285
+
286
+ In case you want or need to control the redirections by yourself instead of allowing NiceHttp to do it, then set ```@http.auto_redirect = false```
287
+
288
+ An example using OpenID authentication:
289
+
290
+ ```ruby
291
+ server = "https://samples.auth0.com/"
292
+ path="/authorize?client_id=kbyuFDidLLm280LIwVFiazOqjO3ty8KH&response_type=code"
293
+
294
+ @http = NiceHttp.new(server)
295
+
296
+ resp = @http.get(path)
297
+
298
+ p "With autoredirection:"
299
+ p "Cookies: "
300
+ p @http.cookies
301
+ p "Code: #{resp.code} #{resp.message} "
302
+ p "*"*40
303
+
304
+ @http2 = NiceHttp.new(server)
305
+ @http2.auto_redirect = false
306
+
307
+ resp = @http2.get(path)
308
+
309
+ p "Without autoredirection:"
310
+ p "Cookies: "
311
+ p @http2.cookies
312
+ p "Code: #{resp.code} #{resp.message} "
313
+
314
+ ```
315
+
316
+ The output:
317
+
318
+ ```
319
+ "With autoredirection:"
320
+ "Cookies: "
321
+ {"/"=>{"auth0"=>"s%3A6vEEwmmIf-9YAG-NjvsOIyZAh-NS97jj.yFSXILdmCov6DRwXjEei3q3eHIrxZxHI4eg4%2BTpUaK4"},
322
+ "/usernamepassword/login"=>{"_csrf"=>"bboZ0koMScwXkISzWaAMTYdY"}}
323
+ "Code: 200 OK "
324
+ "****************************************"
325
+ "Without autoredirection:"
326
+ "Cookies: "
327
+ {"/"=>{"auth0"=>"s%3AcKndc44gllWyJv8FLztUIctuH4b__g0V.QEF3SOobK8%2FvX89iUKzGbfSP4Vt2bRtY2WH7ygBUkg4"}}
328
+ "Code: 302 Found "
329
+
330
+ ```
331
+
332
+ You can see on the next link how to get the OAuth2 token for Microsoft Azure and add it to your Http connection header.
333
+
334
+ https://gist.github.com/MarioRuiz/d3525185024737885c0c9afa6dc8b9e5
335
+
336
+
337
+ ## Send multipart content
338
+
339
+ Example posting a csv file:
340
+
341
+ ```ruby
342
+
343
+ require 'net/http/post/multipart'
344
+ request = {
345
+ path: "/customer/profile/",
346
+ headers: {'Content-Type' => 'multipart/form-data'},
347
+ data: (Net::HTTP::Post::Multipart.new "/customer/profile/",
348
+ "file" => UploadIO.new("./path/to/my/file.csv", "text/csv"))
349
+ }
350
+ response=@http.post(request)
351
+
352
+ ```
353
+
354
+ ## Contributing
355
+
356
+ Bug reports are very welcome on GitHub at https://github.com/marioruiz/nice_http.
357
+
358
+ If you want to contribute please follow [GitHub Flow](https://guides.github.com/introduction/flow/index.html)
359
+
360
+ ## License
361
+
362
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
363
+
364
+
365
+
366
+
366
367
  [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http?ref=badge_large)