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

MethodError when creating form request without headers #1133

Open
Octogonapus opened this issue Nov 28, 2023 · 3 comments
Open

MethodError when creating form request without headers #1133

Octogonapus opened this issue Nov 28, 2023 · 3 comments

Comments

@Octogonapus
Copy link

Octogonapus commented Nov 28, 2023

This works fine (note the header):

HTTP.request(
    "POST",
    "http://localhost:8081",
    Dict("Authorization" => "Bearer as7das876das"),
    HTTP.Form(["files" => open("/dev/random", "r")]),
    response_stream=devnull,
)

However this throws a MethodError:

HTTP.request(
    "POST",
    "http://localhost:8081",
    HTTP.Form(["files" => open("/dev/random", "r")]),
    response_stream=devnull,
)

ERROR: MethodError: no method matching iterate(::HTTP.Forms.Form)

Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen})
   @ Base range.jl:880
  iterate(::Union{LinRange, StepRangeLen}, ::Integer)
   @ Base range.jl:880
  iterate(::T) where T<:Union{Base.KeySet{<:Any, <:Dict}, Base.ValueIterator{<:Dict}}
   @ Base dict.jl:698
  ...

Stacktrace:
  [1] iterate
    @ ./iterators.jl:202 [inlined]
  [2] iterate
    @ ./iterators.jl:201 [inlined]
  [3] mkheaders(h::HTTP.Forms.Form, headers::Vector{Pair{SubString{String}, SubString{String}}})
    @ HTTP.Messages ~/.julia/packages/HTTP/SN7VW/src/Messages.jl:209
  [4] mkheaders(h::HTTP.Forms.Form)
    @ HTTP.Messages ~/.julia/packages/HTTP/SN7VW/src/Messages.jl:209
  [5] mkreqheaders(h::HTTP.Forms.Form, ch::Bool)
    @ HTTP.MessageRequest ~/.julia/packages/HTTP/SN7VW/src/clientlayers/MessageRequest.jl:15
  [6] (::HTTP.MessageRequest.var"#makerequest#3"{HTTP.MessageRequest.var"#makerequest#1#4"{HTTP.RedirectRequest.var"#redirects#3"{HTTP.RedirectRequest.var"#redirects#1#4"{HTTP.HeadersRequest.var"#defaultheaders#2"{HTTP.HeadersRequest.var"#defaultheaders#1#3"{HTTP.CookieRequest.var"#managecookies#4"{HTTP.CookieRequest.var"#managecookies#1#5"{HTTP.RetryRequest.var"#manageretries#3"{HTTP.RetryRequest.var"#manageretries#1#4"{HTTP.ConnectionRequest.var"#connections#4"{HTTP.ConnectionRequest.var"#connections#1#5"{HTTP.TimeoutRequest.var"#timeouts#3"{HTTP.TimeoutRequest.var"#timeouts#1#4"{HTTP.ExceptionRequest.var"#exceptions#2"{HTTP.ExceptionRequest.var"#exceptions#1#3"{typeof(HTTP.StreamRequest.streamlayer)}}}}}}}}}}}}}}}})(method::String, url::URIs.URI, headers::HTTP.Forms.Form, body::Vector{UInt8}; copyheaders::Bool, response_stream::Base.DevNull, http_version::HTTP.Strings.HTTPVersion, verbose::Int64, kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ HTTP.MessageRequest ~/.julia/packages/HTTP/SN7VW/src/clientlayers/MessageRequest.jl:25
  [7] request(stack::HTTP.MessageRequest.var"#makerequest#3"{HTTP.MessageRequest.var"#makerequest#1#4"{HTTP.RedirectRequest.var"#redirects#3"{HTTP.RedirectRequest.var"#redirects#1#4"{HTTP.HeadersRequest.var"#defaultheaders#2"{HTTP.HeadersRequest.var"#defaultheaders#1#3"{HTTP.CookieRequest.var"#managecookies#4"{HTTP.CookieRequest.var"#managecookies#1#5"{HTTP.RetryRequest.var"#manageretries#3"{HTTP.RetryRequest.var"#manageretries#1#4"{HTTP.ConnectionRequest.var"#connections#4"{HTTP.ConnectionRequest.var"#connections#1#5"{HTTP.TimeoutRequest.var"#timeouts#3"{HTTP.TimeoutRequest.var"#timeouts#1#4"{HTTP.ExceptionRequest.var"#exceptions#2"{HTTP.ExceptionRequest.var"#exceptions#1#3"{typeof(HTTP.StreamRequest.streamlayer)}}}}}}}}}}}}}}}}, method::String, url::String, h::HTTP.Forms.Form, b::Vector{UInt8}, q::Nothing; headers::HTTP.Forms.Form, body::Vector{UInt8}, query::Nothing, kw::Base.Pairs{Symbol, Base.DevNull, Tuple{Symbol}, NamedTuple{(:response_stream,), Tuple{Base.DevNull}}})
    @ HTTP ~/.julia/packages/HTTP/SN7VW/src/HTTP.jl:457
  [8] #request#20
    @ ~/.julia/packages/HTTP/SN7VW/src/HTTP.jl:315 [inlined]
  [9] request
    @ ~/.julia/packages/HTTP/SN7VW/src/HTTP.jl:313 [inlined]

The docs seem to indicate this should be okay to do.

  • Julia v1.9.4
  • HTTP.jl v1.10.0
  • MbelTLS.jl v1.1.8
@fredrikekre
Copy link
Member

What in the docs do you refer to? To me it looks like headers is passed everywhere.

@Octogonapus
Copy link
Author

Ah you're right, I missed it in the docs. So this is just meant to be a user error? The exception is confusing, maybe that could be improved?

@fredrikekre
Copy link
Member

Yea I think it is just a victim of duck typing -- headers are allowed to be anything that iterates key-value pairs IIRC, and it isn't really possible to put a type restriction on it. Probably possible to make the error message better though.

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