codegram / hyperclient

HyperClient is a Ruby Hypermedia API client.
MIT License
153 stars 35 forks source link

Handle non-hal+json responses #134

Open espen opened 6 years ago

espen commented 6 years ago

Responses are currently required to be hal+json. The API we consume returns a 201 without data and a http header for a new location when we do a _post. The request is successful but Hyperclient gives a InvalidRepresentationError due to the response not being hal+json.

This works in v0.8.5 but not in the latest version due to https://github.com/codegram/hyperclient/commit/1c7dc4c3d2b0bf311278246f1338de405d08ef84

I'll provide a failing test later.

dblock commented 6 years ago

You should be able to add the right headers or do something like

    conn.request :json
    conn.response :json, content_type: /\bjson$/

If the README is not clear appreciate some PRs.

espen commented 6 years ago

Thanks, overriding response content type works. I assumed the problem was due to the empty body.

client.connection do |conn|
  conn.response :hal_json
end

This is the first hal-json API I'm using so not sure what the norm is. If it is normal to retrieve a response like this with empty body and without content-type then perhaps this user case should be more explicit in the readme (I can provide a PR for that). If this is some uncommon scenario then no worries.

dblock commented 6 years ago

Please PR anything! Documentation never hurt.