#Multipart JSON and binary file not working

1 messages · Page 1 of 1 (latest)

plucky light
#

I'm trying to implement multipart form to post both JSON and a binary file in a single call. The model looks something like this:

struct TestObject: Decodable {
    struct User: Decodable {
        let username: String
    }

    let user: TestObject.User
    let file: Data
}

I handle this in the call as follows:

app.post("test") { req async throws in
    let multipart = try req.content.decode(TestObject.self)
    print(multipart)
    return "Multipart posted, thanks!"
}

I then execute a curl call as follows:

curl -X POST \
-F user={"username":"John"} \
-F [email protected] \
localhost:8080/test

The response I get is {"error":true,"reason":"Value at path 'user' was not of type 'MultipartPart'. Expected dictionary but encountered single value."}. Why is that and how can I solve this?

hoary cloud
#

use Context instead of Decodable, it would be fined in both multipart and json, just import Vapor TestObject: Context

plucky light
hoary cloud
plucky light
hoary cloud
plucky light
hoary cloud
plucky light
#

I see, the multipart is such a weird standard. Apparently it's common to place nested structures in square brackets. So I should indeed do user[username] to indicate the username is part of the user object