Comments (5)
Vapor currently only supports form encoded data. Issue #28 covers supporting JSON requests.
from vapor.
@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.
Does that compile on Ubuntu 14, 15, and OS X?
If so, you should submit that as a PR.
from vapor.
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.
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)
- Updating HTTP server configuration on the fly (ie. TLS settings) HOT 3
- Double encoded post requests after recent update (4.89.3 → 4.91.1) HOT 1
- UserAuthnetication middleware is bypassed when auth object is not used HOT 2
- Query decoding broken in 4.90 HOT 3
- vapor and toolbox compile error on arch linux HOT 1
- double slash in URL no longer matches route handlers HOT 3
- URI semantics are broken HOT 1
- commùit
- Double slash in URLs still breaks route matching with variable parts in vapor 4.92.0 HOT 2
- Trying to build on Ubuntu 20 with swift run and getting Building for debugging... error: emit-module command failed due to signal 9 HOT 2
- Unable to shutdown server HOT 1
- Title
- Vapor URLQueryContainer no longer supports valueless query parameters HOT 4
- Swift Vapor-Save records in Mysql JSON data type during mapping with swift get error- Could not convert MySQL data to String: <MYSQL_TYPE_JSON HOT 6
- Large, streamed request body may result in noSignalReceived preconditionFailure crash HOT 6
- Websocket shouldUpgrade() fail causes empty reply from server HOT 4
- Cannot setup a route returning `[Int: String]` HOT 7
- Log actual port when it's picked by the OS
- 'Flag' URL Query params don't decode into structs HOT 3
- HEAD response should allow non-zero Content-Length HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vapor.