Extract binary file from HTTP request body

Hello,
I’m struggling currently with extracting a binary file from a POST request body.
On the client side I submit a form with a binary file and the enctype=“multipart/form-data”, that seems to work well.

On the server I’m using Oxygen.jl and try to store the binary content into a new file:

@post "/file" function(req)
    open(joinpath("..", "data", "filename"),"w") do f
        write(f, req.body)
    end
    return "file uploaded"
end

When I check the file with a Hex editor I see that there are additional bytes before and after the actual file, in my case an image, like multipart-boundry, filename, content-type,…

But req.body is a Vector{UInt8}, what is the best practice to extract the payload only? Maybe I’m approaching this problem from the wrong side, but I can’t wrap my head around it. :face_with_spiral_eyes:

Thank you for your help!

1 Like

I think you need to parse the form out of the body as described in this thread.

Thank you @contradict,
indeed that is how to do it. In summary I’m able to get the following out of the multipart request array after parsing it, looking at the first element:

    content = HTTP.parse_multipart_form(req)
    content[1].filename
    content[1].contenttype
    content[1].data

The data field is exactly the binary file I was trying to get.