Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let Hyperscript.Node be used for an HTTP.Response #22

Closed
clarkevans opened this issue Sep 25, 2020 · 4 comments
Closed

let Hyperscript.Node be used for an HTTP.Response #22

clarkevans opened this issue Sep 25, 2020 · 4 comments

Comments

@clarkevans
Copy link

clarkevans commented Sep 25, 2020

It'd be great if Hyperscript could work out-of-the-box with HTTP.jl and this does not appear to be the case.

julia> using HTTP
julia> using Hyperscript
julia> @tags html body
julia> HTTP.Response(200, html(body("Hello")))
ERROR: MethodError: no method matching length(::Hyperscript.Node{Hyperscript.HTMLSVG})
Closest candidates are:
  length(::Cmd) at process.jl:639
  length(::Base.Iterators.Flatten{Tuple{}}) at iterators.jl:1061
  length(::BitSet) at bitset.jl:365
  ...
Stacktrace:
 [1] _array_for(::Type{Pair{SubString{String},SubString{String}}}, ::Hyperscript.Node{Hyperscript.HTMLSVG}, ::Base.HasLength) at ./array.jl:677
 [2] mkheaders(::Hyperscript.Node{Hyperscript.HTMLSVG}) at /home/cce/.julia/packages/HTTP/IAI92/src/Messages.jl:205
 [3] HTTP.Messages.Response(::Int64, ::Hyperscript.Node{Hyperscript.HTMLSVG}; body::Array{UInt8,1}, request::Nothing) at /home/cce/.julia/packages/HTTP/IAI92/src/Messages.jl:114
 [4] HTTP.Messages.Response(::Int64, ::Hyperscript.Node{Hyperscript.HTMLSVG}) at /home/cce/.julia/packages/HTTP/IAI92/src/Messages.jl:111
 [5] top-level scope at REPL[11]:1
@SimonDanisch
Copy link
Collaborator

I think this is more an issue for HTTP.jl... If that would fall back to show(io, MIME"text/html"(), x), this would already work nicely!

@clarkevans
Copy link
Author

clarkevans commented Sep 25, 2020

I like your suggestion, but why should HTTP.jl assume MIME"text/html"? Most output these days from web-servers are JSON. For its fallback, perhaps HTTP.jl should attempt to convert(::Vector{UInt8}, ... ).

HTTP.jl is trying to ducktype its response as a vector of characters, does it make sense to support iteration protocol?

Alternatively, I'm curious if HTML{String} being in the default Julia installation could be leveraged. We could ask HTTP.jl to support HTML{...} setting the Content-Type to reflect a MIME type of "text/html" and then rendering content as you suggest. I'm not sure how this could work though; how would Hyperscript.Node signal that it's a HTML data type?

It's not out-of-the-box, but this works...

using HTTP
using Hyperscript
import HTTP: Response

Response(status::Int, node::Hyperscript.Node) = Response(status,
  [SubString("Content-Type") => SubString("text/html; charset=utf-8")];
  body=HTTP.bytes(string(node)))
Response(node::Hyperscript.Node) = Response(200, node)

@SimonDanisch
Copy link
Collaborator

Maybe it should just have Response types, that determine the mime like:

HTMLResponse, JSONResponse

@clarkevans
Copy link
Author

Anyway, I agree this is an issue for HTTP.jl. In fact the 2nd argument in the fallback isn't even content, it's a set of headers. I think that API could use some improvement since that was not obvious to me. Perhaps a traits like interface, e.g. Tables.istable() might be useful in this regard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants