Skip to content

Commit f8e9db6

Browse files
quinnjclaude
andauthored
docs: add a parse_multipart_form usage example (#949) (#1311)
The 2.0 rewrite carried parse_multipart_form forward (with a new Request overload) but its docstring had no example. Add a runnable jldoctest showing boundary-based parsing of a field + a file upload and accessing each part's name/filename/contenttype/data, plus a short server-handler snippet using the Request overload. Supersedes the stale 1.x-targeting PR #949. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent a9e8b86 commit f8e9db6

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

src/http_forms.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,45 @@ handler to inspect file uploads and form fields without re-implementing the
315315
header/body extraction.
316316
317317
Returns `nothing` when either input is missing or the content type is not a
318-
multipart form body.
318+
multipart form body. Each returned `Multipart` exposes the part's `name`,
319+
optional `filename`, `contenttype`, and a readable `data` stream.
320+
321+
# Examples
322+
```jldoctest
323+
julia> body = Vector{UInt8}(
324+
"--boundary\\r\\n" *
325+
"Content-Disposition: form-data; name=\\"greeting\\"\\r\\n\\r\\n" *
326+
"hello\\r\\n" *
327+
"--boundary\\r\\n" *
328+
"Content-Disposition: form-data; name=\\"file\\"; filename=\\"a.txt\\"\\r\\n" *
329+
"Content-Type: text/plain\\r\\n\\r\\n" *
330+
"file contents\\r\\n" *
331+
"--boundary--\\r\\n");
332+
333+
julia> parts = HTTP.parse_multipart_form("multipart/form-data; boundary=boundary", body);
334+
335+
julia> length(parts)
336+
2
337+
338+
julia> parts[1].name, String(read(parts[1].data))
339+
("greeting", "hello")
340+
341+
julia> parts[2].name, parts[2].filename, parts[2].contenttype
342+
("file", "a.txt", "text/plain")
343+
344+
julia> String(read(parts[2].data))
345+
"file contents"
346+
```
347+
348+
Inside a server handler, pass the request directly:
349+
350+
```julia
351+
HTTP.serve!("127.0.0.1", 8080) do request
352+
parts = HTTP.parse_multipart_form(request)
353+
parts === nothing && return HTTP.Response(415, "expected multipart/form-data")
354+
return HTTP.Response(200, "received \$(length(parts)) part(s)")
355+
end
356+
```
319357
"""
320358
function parse_multipart_form(
321359
content_type_header::Union{String,Nothing},

0 commit comments

Comments
 (0)