Alright so constructing the http body by hand is not possible if you want to
post a file, cause big files will take some time to load in memory, and eat
that memory.
I managed to implement a dirty hack in httpclient to insert my headers when
building it.
MultipartEntry* = object
name, content: string
headers*: seq[tuple[name: string, value: string]]
proc format(entry: MultipartEntry, boundary: string): string =
result = "--" & boundary & httpNewLine
result.add("Content-Disposition: form-data; name=\"" & entry.name & "\"")
if entry.isFile:
result.add("; filename=\"" & entry.filename & "\"" & httpNewLine)
result.add("Content-Type: " & entry.contentType & httpNewLine)
for header in entry.headers:
result.add(header.name & ": " & header.value)
result.add(httpNewLine)
else:
for header in entry.headers:
result.add(header.name & ": " & header.value)
result.add(httpNewLine)
result.add(httpNewLine & httpNewLine & entry.content)
proc add*(p: MultipartData, name, content: string, headers:
seq[tuple[name:string, value: string]] = @[("","")], filename: string = "",
contentType: string = "", useStream = true) =
...
var entry = MultipartEntry(
name: name,
content: content,
headers: headers,
proc addFiles*(p: MultipartData, xs: openArray[tuple[name, file: string]],
headers: seq[tuple[name:string, value:string]] = @{"":""},
mimeDb = newMimetypes(), useStream = true):
MultipartData {.discardable.} =
Run
It would be nice if someone could rewrite it properly and submit the changes to
nim git.