local-ch / lhc

🚀 Advanced HTTP Client for Ruby. Fueled with interceptors.
GNU General Public License v3.0
43 stars 1 forks source link

Simplify making JSON requests #120

Closed 10xSebastian closed 6 years ago

10xSebastian commented 6 years ago

MAJOR (not backwards compatible)

Before

Even though JSON already has been our default format when performing HTTP requests (see here), we were still obliged to translate the body option to json, before passed to LHC, when performing requests having an HTTP body.

LHC.post('http://datastore', body: { name: 'Steve' }.to_json)

This also led to inelegant implementations, assuming or hard-code checking for "application/json" or a json-like string in the body – e.g. when compiling the url (see here) – and even though we have the http message "format" abstracted in LHC (see here)

Now

Body translation is now also performed by LHC. So we can continue working with plain ruby objects, and leave the rest to the selected LHC format or json as default. Which allows to cleanup the internal code, and prevents us from using .to_json everytime we pass a body.

LHC.post('http://datastore', body: { name: 'Steve' })

How to migrate

Simply remove all .to_json when handing over ruby objects to LHC as a body.

Change

LHC.post('http://datastore', body: { name: 'Steve' }.to_json)

to

LHC.post('http://datastore', body: { name: 'Steve' })
10xSebastian commented 6 years ago

@scan It always did. See: https://github.com/local-ch/lhc/blob/master/lib/lhc/formats/json.rb#L7-L8 https://github.com/local-ch/lhc/blob/master/lib/lhc/request.rb#L22