Trying to implement a few IPFS API calls, specifically this one: https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-add
Basically I need to implement the "nocopy" option of the above linked API call, which in curl looks like this: curl 'http://127.0.0.1:5001/api/v0/add?nocopy=true' -F 'file=@SOMEFILE;headers="Abspath: /home/magik6k/full/path/to/SOMEFILE"' (from https://github.com/ipfs/kubo/issues/4558#issuecomment-462570466)
I have tried various things, wasted half a day on this, and still no success. I am not good with http and its workings. Please provide httpclient code for the equivalent curl command. No, I can't use libcurl because it can't be used asynchronously in nim, afaik.
thank you
A little update after getting the idea that I can easily debug with nc: curl "correct" request:
--------------------------c6d4d41bf1581405
Content-Disposition: form-data; name="file"; filename="1.txt"
Content-Type: text/plain
Abspath: /home/sgm/1.txt
123
--------------------------c6d4d41bf1581405--
nim result, with the code below:
--4292486321577947087
Content-Disposition: form-data; name="file"; filename="1.txt"
Content-Type: text/plain
123
--4292486321577947087
Content-Disposition: form-data; name="headers"
Abspath: /home/sgm/1.txt
--4292486321577947087--
It seems that this is currently not possible with the current api.
What you can do however is to construct the correct http body by hand.
You should also open an incident on github.
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.} =
It would be nice if someone could rewrite it properly and submit the changes to nim git.