Skip to content

Commit 8dc7039

Browse files
authored
Add AbstractDict for request bodies (#1029)
1 parent a3a5768 commit 8dc7039

File tree

5 files changed

+9
-9
lines changed

5 files changed

+9
-9
lines changed

src/HTTP.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ as an additional keyword argument to the request.
8989
9090
`body` can be a variety of objects:
9191
92-
- a `Dict` or `NamedTuple` to be serialized as the "application/x-www-form-urlencoded" content type
92+
- an `AbstractDict` or `NamedTuple` to be serialized as the "application/x-www-form-urlencoded" content type
9393
- any `AbstractString` or `AbstractVector{UInt8}` which will be sent "as is" for the request body
9494
- a readable `IO` stream or any `IO`-like type `T` for which
9595
`eof(T)` and `readavailable(T)` are defined. This stream will be read and sent until `eof` is `true`.
9696
This object should support the `mark`/`reset` methods if request retires are desired (if not, no retries will be attempted).
97-
- Any collection or iterable of the above (`Dict`, `AbstractString`, `AbstractVector{UInt8}`, or `IO`)
97+
- Any collection or iterable of the above (`AbstractDict`, `AbstractString`, `AbstractVector{UInt8}`, or `IO`)
9898
which will result in a "chunked" request body, where each iterated element will be sent as a separate chunk
9999
- a [`HTTP.Form`](@ref), which will be serialized as the "multipart/form-data" content-type
100100

src/Messages.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ function retryable end
277277
supportsmark(x) = false
278278
supportsmark(x::T) where {T <: IO} = length(Base.methods(mark, Tuple{T}, parentmodule(T))) > 0 || hasfield(T, :mark)
279279

280-
# request body is retryable if it was provided as "bytes", a Dict or NamedTuple,
280+
# request body is retryable if it was provided as "bytes", an AbstractDict or NamedTuple,
281281
# or a chunked array of "bytes"; OR if it supports mark() and is marked
282-
retryablebody(r::Request) = (isbytes(r.body) || r.body isa Union{Dict, NamedTuple} ||
282+
retryablebody(r::Request) = (isbytes(r.body) || r.body isa Union{AbstractDict, NamedTuple} ||
283283
(r.body isa Vector && all(isbytes, r.body)) || (supportsmark(r.body) && ismarked(r.body)))
284284

285285
# request is retryable if the body is retryable, the user is allowing retries at all,
@@ -581,7 +581,7 @@ const BODY_SHOW_MAX = Ref(1000)
581581
The first chunk of the Message Body (for display purposes).
582582
"""
583583
bodysummary(body) = isbytes(body) ? view(bytes(body), 1:min(nbytes(body), BODY_SHOW_MAX[])) : "[Message Body was streamed]"
584-
bodysummary(body::Union{Dict, NamedTuple}) = URIs.escapeuri(body)
584+
bodysummary(body::Union{AbstractDict, NamedTuple}) = URIs.escapeuri(body)
585585
function bodysummary(body::Form)
586586
if length(body.data) == 1 && isa(body.data[1], IOBuffer)
587587
return body.data[1].data[1:body.data[1].ptr-1]

src/clientlayers/DefaultHeadersRequest.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function defaultheaderslayer(handler)
3838
if !hasheader(headers, "Content-Type") && req.body isa Form && req.method in ("POST", "PUT", "PATCH")
3939
# "Content-Type" => "multipart/form-data; boundary=..."
4040
setheader(headers, content_type(req.body))
41-
elseif !hasheader(headers, "Content-Type") && (req.body isa Dict || req.body isa NamedTuple) && req.method in ("POST", "PUT", "PATCH")
41+
elseif !hasheader(headers, "Content-Type") && (req.body isa Union{AbstractDict, NamedTuple}) && req.method in ("POST", "PUT", "PATCH")
4242
setheader(headers, "Content-Type" => "application/x-www-form-urlencoded")
4343
end
4444
if decompress === nothing || decompress

src/clientlayers/StreamRequest.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ function writebodystream(stream, body::IO)
9494
write(stream, body)
9595
end
9696

97-
function writebodystream(stream, body::Union{Dict, NamedTuple})
97+
function writebodystream(stream, body::Union{AbstractDict, NamedTuple})
9898
# application/x-www-form-urlencoded
9999
write(stream, URIs.escapeuri(body))
100100
end
101101

102102
writechunk(stream, body::IO) = writebodystream(stream, body)
103-
writechunk(stream, body::Union{Dict, NamedTuple}) = writebodystream(stream, body)
103+
writechunk(stream, body::Union{AbstractDict, NamedTuple}) = writebodystream(stream, body)
104104
writechunk(stream, body) = write(stream, body)
105105

106106
function readbody(stream::Stream, res::Response, decompress::Union{Nothing, Bool})

src/multipart.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ end
7878
7979
Construct a request body for multipart/form-data encoding from `data`.
8080
81-
`data` must iterate key-value pairs (e.g. `Dict` or `Vector{Pair}`) where the key/value of the
81+
`data` must iterate key-value pairs (e.g. `AbstractDict` or `Vector{Pair}`) where the key/value of the
8282
iterator is the key/value of each mutipart boundary chunk.
8383
Files and other large data arguments can be provided as values as IO arguments: either an `IOStream`
8484
such as returned via `open(file)`, or an `IOBuffer` for in-memory data.

0 commit comments

Comments
 (0)