Comments (12)
The current state of response parsing is up in the air. See #39.
Right now the body should get parsed automatically if the JSON gem is loaded. In the new API, I'd propose:
HTTP.accept(:json).get(url).parse_body
from http.
looks good. could parse() accept a Proc to override the default handler?
from http.
Well, the point of #parse_body
is it can know the Content-Type
from the response and choose an appropriate parser. I'm not sure what an arbitrary Proc would get you... you could use #body.to_s
to obtain a string and parse it however you want at that point ;)
from http.
yeah, #to_s
and checking the content-type manually works. my idea was flawed because i was assuming 'Accept' would guarantee a response of the same Content-Type but of course that isn't true. I was thinking about someone who wanted to use a (custom) JSON parser whenever that Content-Type
is encountered though. is it going to be possible to extend the content-types that the HTTP library will understand and automatically parse as JSON?
maybe company X has defined their own content-type to describe the schema of a JSON document and would like to take advantage of the letting the http
library parse the body. otherwise they're back to checking the Content-Type manually & doing the parsing themselves (i would figure).
from http.
@robgleeson the MIME type handlers are pluggable. See:
https://github.com/tarcieri/http/blob/master/lib/http/mime_types/json.rb
Arguably they could have a better API
from http.
yeah, i think there should probably be an easy way to alias a custom content-type as being understood as JSON(or XML, CSV, or …) by the http lib if it has predefined handlers for JSON/XML/CSV. it would also be nice if I could extend the http
lib to understand new content-types and define custom handlers so that the http lib interface doesn't end up being inconsistent or needing a different API for content-type's it doesn't account for.
from http.
by "different API" I mean if I think I might get a content-type that http
lib doesn't understand I need to check the content-type and handle the parsing myself, even if response could have a content-type that is understood, there's a chance it doesn't so i cannot rely on #parsed
.
from http.
I also like this style of registering API adapters:
https://github.com/copiousfreetime/qup/blob/master/lib/qup/adapter/redis.rb#L10
So it would work like:
class MyHandler < HTTP::MimeType
register 'application/myformat'
def parse(data)
do_awesome_stuff(data)
end
end
from http.
If that's all you want, you could do it like this:
response = HTTP.accept(:json).get(url)
body = response.body.to_s
case response['Content-Type']
when 'application/myformat'
...
end
from http.
yeah, I could, but then the parsed
doesn't handle that case, so I can't use it (but I'd like to).
I'd be cool with registering new types as a subclass as long as I can register the type with http
so that it is understood and used when #parsed
is called.
from http.
something like HTTP::Handler["application/myformat"] = MyHandler.new
might be nice (that'd let me re-assign and re-use other handlers that the #parsed
method uses).
from http.
gonna close, original idea didn't make sense.
from http.
Related Issues (20)
- Requests to an IPv6 address result in: HTTP::ConnectionError (failed to connect: getaddrinfo: Name or service not known) HOT 13
- Incorrect method documentation? HOT 2
- feature request: happy eyeballs
- Can we update to llhttp-ffi 0.5.0 HOT 2
- access ssl certificate details? HOT 4
- Issues running it on Mac Intel (12.6) HOT 3
- SSLERROR occurred when running http method in the executable file: certificate verify failed. HOT 1
- 403 and 404 responses for valid URLs HOT 7
- raise error when invalid hash is passed to timeout HOT 1
- Are you open to include rspec_matchers? HOT 18
- File descriptor / socket leak when timeout occurs during initialization
- How to query the remote server address? HOT 1
- Make HTTP::Feature work like rack middleware HOT 10
- Remove auth header after following a redirect to a different origin
- Support HTTP headers streaming HOT 2
- Timeout redesign for 6.x HOT 4
- Fix Base64 dependency warning HOT 4
- Version 5.x bump? HOT 3
- project level timeout HOT 2
- `HTTP::Response::Body#each` and `BUFFER_SIZE`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http.