local-ch / lhc

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

Plain and Multipart #141

Closed 10xSebastian closed 6 years ago

10xSebastian commented 6 years ago

MINOR

This PR adds the multipart and plain (not formatted) chain to LHC basic methods.

Formats

You can use any of the basic methods in combination with a format like json:

LHC.json.get(options)

Currently supported formats: json, multipart, plain (for no formatting)

If formats are used, headers for Content-Type and Accept are set by LHC, but also http bodies are translated by LHC, so you can pass bodies as ruby objects:

LHC.json.post('http://slack', body: { text: 'Hi there' })
# Content-Type: application/json
# Accept: application/json
# Translates body to "{\"text\":\"Hi there\"}" before sending

Default format

If you use LHC's basic methods LHC.get, LHC.post etc. without any explicit format, JSON will be chosen as the default format.

Unformatted requests

In case you need to send requests without LHC formatting headers or the body, use plain:

LHC.plain.post('http://endpoint', body: { weird: 'format%s2xX' })
Upload with LHC

If you want to upload data with LHC, it's recommended to use the multipart format:

response = LHC.multipart.post('http://upload', body: { file })
response.headers['Location']
# Content-Type: multipart/form-data
# Leaves body unformatted