Skip to content

Commit

Permalink
Merge pull request #142 from rdalverny/fix_body_encoding
Browse files Browse the repository at this point in the history
Serialize to JSON when content type is set to JSON
  • Loading branch information
sethpollack authored Mar 21, 2019
2 parents 7c538e4 + 26e5eb3 commit 5e06ab1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,19 @@ When calling any of the methods above, you can pass request headers to be used.
get 'http://example.com/api/v1/my_api', { 'x-auth-token' => 'my_token' }
```

For requests that require a body (`post`, `put`, `patch`) you can pass the body as a hash as well:
For requests that require a body (`post`, `put`, `patch`) you can pass the body as well:

```ruby
post 'http://example.com/api/v1/my_api', { :name => 'John Doe' }, { 'x-auth-token' => 'my_token' }
```

The body may be any JSON-serializable type, as long as you want to post `application/json` content type.
You may set a different content type and post a string body this way:

```ruby
post 'http://example.com/api/v1/my_api', "Hello there!", { content_type: 'text/plain' }
```

For requests that require Query params you can pass a params hash into headers.

```ruby
Expand Down
7 changes: 6 additions & 1 deletion lib/airborne/rest_client_requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def make_request(method, url, options = {})
res = if method == :post || method == :patch || method == :put
begin
request_body = options[:body].nil? ? '' : options[:body]
request_body = request_body.to_json if options[:body].is_a?(Hash)
request_body = request_body.to_json if is_json_request(headers)
RestClient.send(method, get_url(url), request_body, headers)
rescue RestClient::Exception => e
e.response
Expand All @@ -24,6 +24,11 @@ def make_request(method, url, options = {})

private

def is_json_request(headers)
header = headers.fetch(:content_type)
header == :json || /application\/([a-zA-Z0-9\.\_\-]*\+?)json/ =~ header
end

def base_headers
{ content_type: :json }.merge(Airborne.configuration.headers || {})
end
Expand Down
21 changes: 21 additions & 0 deletions spec/airborne/client_requester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,25 @@
expect(RestClient).to have_received(:send)
.with(:get, 'http://www.example.com/foo', { content_type: 'text/plain' })
end

it 'should serialize body to json when :content_type is (default) :json' do
post '/foo', { test: 'serialized' }

expect(RestClient).to have_received(:send)
.with(:post, 'http://www.example.com/foo', '{"test":"serialized"}', { content_type: :json })
end

it 'should serialize body to json when :content_type is any enhanced JSON content type' do
post '/foo', { test: 'serialized' }, { content_type: 'application/vnd.airborne.2+json' }

expect(RestClient).to have_received(:send)
.with(:post, 'http://www.example.com/foo', '{"test":"serialized"}', { content_type: 'application/vnd.airborne.2+json' })
end

it 'should not serialize body to json when :content_type does not match JSON' do
post '/foo', { test: 'not serialized' }, { content_type: 'text/plain' }

expect(RestClient).to have_received(:send)
.with(:post, 'http://www.example.com/foo', { test: 'not serialized' }, { content_type: 'text/plain' })
end
end

0 comments on commit 5e06ab1

Please sign in to comment.