Group Group Group Group Group Group Group Group Group

Upload Data | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/10376245-networking-with-urlsession/lessons/17

When I follow the instructions in the video to create the UploadServer Vapor project, I end up with a project whose routes.swift file looks like this:

import Vapor

func routes(_ app: Application) throws {
    app.get { req in
        return "It works!"
    }

    app.get("hello") { req -> String in
        return "Hello, world!"
    }
}

I tried to adapt the code shown in the video by adding the VideoCourse struct as shown and the following function:

app.post("upload") { req -> Future<HTTPStatus> in
    return try! req.content.decode(VideoCourse.self).map(to: HTTPStatus.self) { course in
        print(course.name)
        print(course.language)
        print(course.version)
        return .ok
    }
}

However, this doesn’t compile; I get the error “Use of undeclared type ‘Future’”. Besides that, my project contains only one scheme named UploadServer. I don’t see the Run scheme shown in the video.

Any ideas about what I’m doing wrong?

When I create my Vapor project, I have more options to choose that what’s on the tutorial.

No matter which option I choose, I’m unable to continue with the lesson. Any thoughts on what I need to do differently?

Any advice is appreciated. Thank you.

First off, what version of vapor are you running? When I recorded this, Vapor 4 was just released so it may act different from Vapor 3.

That’s the difference. I’m on 4.

If you’re on version 4 use this code instead… works for me…
You will still need the enum defined at the top called VideoCourse.

  app.on(.POST, "upload") { req -> HTTPResponseStatus in
    let course = try req.content.decode(VideoCourse.self)
    print(course.name)
    print(course.language)
    print(course.version)
    return .ok
  }

Passing this in JSON:
{
“language”: “en”,
“name”: “Ed”,
“version”: 4
}

Shows the following in the console:
[ INFO ] POST /upload
Ed
en
4.0

1 Like

@cupofjoe Thank you for sharing you solution - much appreciated!

@jayt @jefferymasontulsatec Do you still have issues with this?