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.

Screen Shot 2020-06-26 at 12.07.38 PM

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?

@shogunkaramazov Just tried the recommended solution of @cupofjoe, works for me. Thanks!

For Brian Moakley re: URLSession, session 16, Install Vapor. One small procedural detail for XCode 11 systems that are relatively new: The XCode => Preferences => Locations tab needs to have Command Line Tools selected. (in my case, Xcode 11.5 is the only option). if not, the Terminal vapor install process fails with error - unable to find utility ā€œxctestā€, not a developer tool or in PATH. Select the Command Line Tools option, then reinstall, the error does not appear, and vapor is installed.

Also for Brian Moakley re: URLSession, session 17 Upload Data. At the start you enter ā€œvapor new UploadServerā€. It asks ā€œWould you like to use Fluent?ā€ which does not appear in your instructional video. Should the answer be Yes or No ?

Continuing on my Macbook Pro running MacOS Catalina 10.15.5 and continuing on session 17. I replied No to using Fluent. Then progressed to launching XCode (11.5) and opening routes.swift. The Package.swift file says, btw, says this is swift-tools-version: 5.2, vapor is 4.0.0.
The code in the routes.swift routes function differs slightly from the video when XCode opens up for this first time. it shows ā€œroutes(_ app: Application)ā€ instead of the videoā€™s "routes(_ router: Router). No matter. When you try to compile it, it states there is one issue with Vapor: Target Integrity. Crypto requires platform version 13.0 for the IOS platform, but this target supports 8.0. As a new newbie to the Apple universe, I am not readily finding where I adjust the targeting. The same error occurs for XCTVapor.

The videos were recorded using the Vapor 3 framework so the code is gong to be different. Once we released the videos, vapor 4 dropped. Iā€™ll see what I can do about updating this to use Vapor 4.

The solution to the Crypto issue here came by catching, by chance, a glance at the very upper edge of the videoā€¦ it was compiling for the Mac, not for the iPhone we had been simulating. I changed the target from an iPhone to my Macbook, and it compiled.

Now, since I am on Vapor 4.0, I need to modify the code in the video with the corrected code in this forum and extending it some. The compiler rejects the video ā€œRouterā€ reference but accepts the original ā€œApplicationā€ references when I change the app.post(ā€œuploadā€) to app.on(.POST, ā€œuploadā€), and change the router.get lines to app.get.

And by the way, if you ever remake this video, a higher resolution would be nice. The playground portion of the video is pretty fuzzy. (Been programming since 1966 and I sure appreciate high-res images over fuzzy ones. And you know what? The Mac is somewhat of an improvement over the teletype devices - with no screens at all - we used as computer consoles back then.)

Thanks, Brian! I am moving on to the next session. Everything works okay, except for the final step: I could not get this playground upload to actually talk to the server. Close, but no cigar. Could be my system is too ā€œvanillaā€ at this point, is missing something minor that your system has already resolved at some point in the past.

Brian, have advanced to your session 23ā€¦ Connect to a websocket. I do exactly as your video indicates, but the result is radically different. Vapor created the project and launched XCode and the server project. I opened configure.swift file, and see it contains just one single line (try routes(app)), not the many lines of code in your example.

Please advise.

Thank you swift_5_courses for sharing, it helped meā€‹:grinning::+1:t3:

Brian, here is something to fix in the Vapor installation sequence. The video takes us to Homebrew and checks what version of Vapor it is hosting. The video shows it found Vapor version 3.2.0. However, that is not Vapor for Swift. That VAPOR is the Visualization and Analysis Platform for Ocean, Atmosphere, and Solar Researchers.
(The website URL shown on your video is GitHub - NCAR/VAPOR: VAPOR is the Visualization and Analysis Platform for Ocean, Atmosphere, and Solar Researchers. Check it out!)

Meanwhile, the formulae area in Homebrew, below the cask area mentioned above, currently indicates they host ā€œVaporā€ version 18.0.0. They indicate this is the command-line tool for Vapor (Server-side Swift web framework). After following the video instructions, and then typing in ā€œbrew info vaporā€, it informs me it did install 18.0.0 (bottled), HEAD.

1 Like

Hey there, thanks for the heads up. Iā€™ve gone ahead and updated the install video. Iā€™m in the process of updating this video for Vapor 4 (as well as the websocket video). I appreciate the feedback. Cheers!