diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f637c61..3c9108cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api ## Unreleased + +- [#1098](https://github.com/Shopify/shopify-api-ruby/pull/1098) Gracefully handle HTTP 204 repsonse bodies - [#1104](https://github.com/Shopify/shopify-api-ruby/pull/1104) Allow api version overrides. ## Version 12.4.0 diff --git a/lib/shopify_api/clients/http_client.rb b/lib/shopify_api/clients/http_client.rb index e19de2abb..34ba499ca 100644 --- a/lib/shopify_api/clients/http_client.rb +++ b/lib/shopify_api/clients/http_client.rb @@ -48,7 +48,7 @@ def request(request) body: request.body.class == Hash ? T.unsafe(request.body).to_json : request.body, ), HTTParty::Response) - body = res.body.empty? ? {} : JSON.parse(res.body) + body = res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body) response = HttpResponse.new(code: res.code.to_i, headers: res.headers.to_h, body: body) if response.headers["x-shopify-api-deprecated-reason"] diff --git a/test/clients/http_client_test.rb b/test/clients/http_client_test.rb index b6e01388f..5bbb7ce51 100644 --- a/test/clients/http_client_test.rb +++ b/test/clients/http_client_test.rb @@ -71,6 +71,20 @@ def test_request_with_empty_response_body verify_http_request end + # It doesn't really make sense for an HTTP body to ever be `nil`, but + # the underlying HTTParty library seems to use `nil` for the response + # body in situations where it doesn't need to parse it, e.g. when the + # response status is 204. + def test_request_with_nil_response_body + @success_body = {} + + stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}") + .with(body: @request.body.to_json, query: @request.query, headers: @expected_headers) + .to_return(body: "", headers: @response_headers, status: 204) + + verify_http_request + end + def test_request_with_no_access_token @session = ShopifyAPI::Auth::Session.new(shop: @shop) @client = ShopifyAPI::Clients::HttpClient.new(session: @session, base_path: @base_path)