2015-11-21 16:28:34 -06:00
# httparty
Makes http fun again!
## Table of contents
2016-11-14 10:08:50 -08:00
- [Parsing JSON ](#parsing-json )
2015-11-21 16:28:34 -06:00
- [Working with SSL ](#working-with-ssl )
2016-11-14 10:08:50 -08:00
## Parsing JSON
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:
2019-02-18 15:36:03 +01:00
```ruby
2016-11-14 10:08:50 -08:00
response = HTTParty.get('http://example.com', format: :plain)
JSON.parse response, symbolize_names: true
```
2015-11-21 16:28:34 -06:00
## Working with SSL
You can use this guide to work with SSL certificates.
#### Using `pem` option
```ruby
# Use this example if you are using a pem file
class Client
2017-06-20 16:25:22 +08:00
include HTTParty
2015-11-21 16:28:34 -06:00
2017-06-20 16:25:22 +08:00
base_uri "https://example.com"
pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
2015-11-21 16:28:34 -06:00
end
```
#### Using `pkcs12` option
```ruby
# Use this example if you are using a pkcs12 file
class Client
2017-06-20 16:25:22 +08:00
include HTTParty
2015-11-21 16:28:34 -06:00
2017-06-20 16:25:22 +08:00
base_uri "https://example.com"
pkcs12 File.read("#{File.expand_path('.')}/path/to/certs/cert.p12"), "123456"
2015-11-21 16:28:34 -06:00
end
```
#### Using `ssl_ca_file` option
```ruby
# Use this example if you are using a pkcs12 file
class Client
2017-06-20 16:25:22 +08:00
include HTTParty
2015-11-21 16:28:34 -06:00
2017-06-20 16:25:22 +08:00
base_uri "https://example.com"
ssl_ca_file "#{File.expand_path('.')}/path/to/certs/cert.pem"
2015-11-21 16:28:34 -06:00
end
```
#### Using `ssl_ca_path` option
```ruby
# Use this example if you are using a pkcs12 file
class Client
2017-06-20 16:25:22 +08:00
include HTTParty
base_uri "https://example.com"
ssl_ca_path '/path/to/certs'
2015-11-21 16:28:34 -06:00
end
```
2021-05-13 11:12:34 -05:00
You can also include all of these options with the call:
2015-11-21 16:28:34 -06:00
```ruby
class Client
2017-06-20 16:25:22 +08:00
include HTTParty
base_uri "https://example.com"
def self.fetch
2020-03-25 08:41:18 +01:00
get("/resources", pem: File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), pem_password: "123456")
2017-06-20 16:25:22 +08:00
end
2015-11-21 16:28:34 -06:00
end
```
### Avoid SSL verification
2021-05-13 11:12:34 -05:00
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:
2015-11-21 16:28:34 -06:00
```ruby
2017-06-20 16:25:22 +08:00
# Skips SSL certificate verification
2015-11-21 16:28:34 -06:00
class Client
2017-06-20 16:25:22 +08:00
include HTTParty
base_uri "https://example.com"
pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
def self.fetch
get("/resources", verify: false)
# You can also use something like:
# get("resources", verify_peer: false)
end
2015-11-21 16:28:34 -06:00
end
2019-02-18 15:36:03 +01:00
```