Skip to content

Commit 47c7dc7

Browse files
committed
expose reponse header and raw body
1 parent 535a31a commit 47c7dc7

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/imagekitio/request.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ def request(method, url, headers = create_headers, payload = nil)
4242
http.use_ssl = (uri.scheme == 'https')
4343
req = Net::HTTP::Post::Multipart.new uri.path, payload, headers
4444
resp = http.request(req)
45-
if resp.code.to_i == 400
46-
raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 400, body: resp.body)
47-
end
48-
if resp.code.to_i == 403
49-
raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 403, body: resp.body)
45+
response[:headers] = resp.to_hash
46+
response[:raw_body] = resp.body
47+
resp_c = resp.code.to_i
48+
if [400, 403].include?(resp_c)
49+
raise RestClient::ExceptionWithResponse, OpenStruct.new({ body: resp.body, code: resp_c })
5050
end
5151
else
5252
resp = RestClient::Request.new(method: method,
5353
url: url,
5454
headers: headers,
5555
payload: payload).execute
56+
response[:headers] = resp.raw_headers
57+
response[:raw_body] = resp.body
5658
end
5759
if (resp.code.to_i >= 200) && (resp.code.to_i < 204)
5860
content_type = resp.respond_to?(:headers) ? resp.headers[:content_type] : resp.content_type

test/imagekit/request_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,23 @@
8585
expect(response).to have_key(:error)
8686
expect(response[:error]).to eq({"message"=>"Your account cannot be authenticated.", "help"=>"For support kindly contact us at [email protected] ."})
8787
end
88+
89+
it 'test_response_headers' do
90+
stub_request(:post, 'https://www.exampleservererror/upload').to_return(status: 200, body: "{\"message\":\"Success.\"}", headers: {"strict-transport-security"=>["max-age=15552000"],
91+
"x-ik-requestid"=>["39e19bd4-c9c3-4025-453e-a13e6a412aa0"],
92+
"content-type"=>["application/json; charset=utf-8"],
93+
"x-request-id"=>["39e19bd4-c9c3-4025-a043-a13e6a412aa0"]})
94+
response = @request_obj.request(:post, 'https://www.exampleservererror/upload', nil, { multipart: true })
95+
expect(response[:headers]).to eq({"strict-transport-security"=>["max-age=15552000"],
96+
"x-ik-requestid"=>["39e19bd4-c9c3-4025-453e-a13e6a412aa0"],
97+
"content-type"=>["application/json; charset=utf-8"],
98+
"x-request-id"=>["39e19bd4-c9c3-4025-a043-a13e6a412aa0"]})
99+
end
100+
101+
it 'test_response_raw_body' do
102+
stub_request(:post, 'https://www.exampleservererror/upload').to_return(status: 200, body: "{\"message\":\"Success.\"}", headers: {"strict-transport-security"=>["max-age=15552000"], "content-type"=>["application/json; charset=utf-8"]})
103+
response = @request_obj.request(:post, 'https://www.exampleservererror/upload', nil, { multipart: false })
104+
expect(response[:raw_body]).to eq("{\"message\":\"Success.\"}")
105+
end
88106
end
89107
end

0 commit comments

Comments
 (0)