vapor/vapor

How do I use Vapor4 to send multipart/form-data requests?

iWECon opened this issue · 2 comments

iWECon commented
@discardableResult
    public func sendVideo(uri: URI, caption: String, mediaFilePath: String, name: String) async throws -> Lookup {
        
        // ‼️ the media file is `mp4`
        let fileURL = URL(fileURLWithPath: mediaFilePath)
        let fileData = try Data(contentsOf: fileURL)
        
        let response = try await app.client.post(uri, beforeSend: { cr in
            try cr.content.encode(Lookup([
                "chat_id": name,
                "video": fileData,
                "caption": caption
            ]), as: .formData)
        })
        return try response.content.decode(Lookup.self) // < ❌ always get invalid `video` in your request
    }
iWECon commented

Resolved.