Giter Site home page Giter Site logo

post request body about vapor HOT 5 CLOSED

vapor avatar vapor commented on April 27, 2024
post request body

from vapor.

Comments (5)

tanner0101 avatar tanner0101 commented on April 27, 2024

Vapor currently only supports form encoded data. Issue #28 covers supporting JSON requests.

from vapor.

loganwright avatar loganwright commented on April 27, 2024

@tannernelson Cool, in the meantime, here's two different examples that work:

In case it helps, these are both functioning for me:

extension Request {
    public var jsonBody: Any? {
        var bytes = body
        let data = NSData(bytes: &bytes, length: body.count)
        return try? NSJSONSerialization
            .JSONObjectWithData(data, options: .AllowFragments)
    }
}

Or w/ pure json dependency:

.Package(url: "https://github.com/gfx/Swift-PureJsonSerializer", majorVersion: 1)
extension Request {
    public var jsonBody: Json? {
        return try? Json.deserialize(body)
    }
}

from vapor.

tanner0101 avatar tanner0101 commented on April 27, 2024

Does that compile on Ubuntu 14, 15, and OS X?

If so, you should submit that as a PR.

from vapor.

loganwright avatar loganwright commented on April 27, 2024

@tannernelson

They both work on Heroku box, but the NSJSON version is relatively useless for anything but printing. It doesn't like it if I cast to [String : Any], and it doesn't like it if I cast to [String : AnyObject] on linux.

I think until Foundation adds official support, it might be worth using, or pointing to a 3rd party dependency. The PureJson version above runs on all instances and in my opinion, is a better way to deal with Json.

If you're interested in that, I can make a pull that includes the dependency. It's a little slower than NSJSON, but you also get the added benefit of being able to return:

return json.serialize(.PrettyPrint)

Which serializes more effectively

from vapor.

tanner0101 avatar tanner0101 commented on April 27, 2024

The NSJSON from Foundation won't work because it relies on the automatic casting of any Array or Dictionary to an NSArray or NSDictionary. I don't know if that's something that will ever be added to Linux.

So I think a dependency makes sense for serialization on Linux. If Apple does decide to include Foundation support at some point it can be removed.

from vapor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.