Comments (6)
@anhar You can use the Method parser:
Route(.case(HouseRoute.create)) {
Method.post
Path { "houses" }
}
I found out that the root cause of this failing is because of the following line in Route.swift
The parser uses the GET
method by default if it isn't set on the request.
The swift-url-routing Documentation is a good place to start to see what's available. It might be worth checking out the benchmark suite or even the PointFreeRouter for some example use cases.
from vapor-routing.
Okay so after doing some digging I found out that the root cause of this failing is because of the following line in Route.swift in the swift-url-routing library.
I forked both repositories and updated the method to this which solved the issue:
@inlinable
public func parse(_ input: inout URLRequestData) throws -> Parsers.Output {
let output = try self.parsers.parse(&input)
if input.method != nil {
if input.method == "GET" {
try Method.get.parse(&input)
}
else if input.method == "POST" {
try Method.post.parse(&input)
}
}
try PathEnd().parse(input)
return output
}
However I'm not sure how the underlying library works and if you want to just go ahead and hardcode the different HTTP methods in a beautiful nested if else statement 🤷♂️
from vapor-routing.
The parser uses the GET method by default if it isn't set on the request.
That's not true though, the URLRouting.URLRequestData
object has its method
property set to POST
. See the screenshot below:
This code snippet sets the method
property to nil
somewhere down the callstack:
Route(.case(HouseRoute.create)) {
Method.post
Path { "houses"; }
}
from vapor-routing.
@anhar You're right. I meant the parser tries to parse the GET
method by default. That way Method.get
can be omitted for GET
(and HEAD
) requests.
The Method
parser, will parse the method
from the request data which sets it to nil
.
Does using Method.post
solve the problem or is it still not working as expected?
from vapor-routing.
Hi @anhar, Pat is correct in this situation, and your PR is not how the library should work. It may seem weird, but this logic in Route
's parse method is correct:
public func parse(_ input: inout URLRequestData) throws -> Parsers.Output {
let output = try self.parsers.parse(&input)
if input.method != nil {
try Method.get.parse(&input)
}
try PathEnd().parse(input)
return output
}
In this method, input
is the incoming URL request to your server. If after running the parser on this request the method
is still not-nil
, then it means that your parser chose not to parse the HTTP method ever (this is because the Method
parser consumes the method
if it succeeds). So, if that is the case then we force the method to be a GET or HEAD.
So, in your first screenshot it is completely expected that input.method
would be POST because your parser did not try parsing the method. And in your second screenshot it is correct for input.method
to be nil
because then you did start using Method.post
to parse the method.
With that said, I do believe you want to specify Method.post
in your router for creating a house:
let houseRouter = OneOf {
Route(.case(HouseRoute.house)) {
Path { "houses"; UUID.parser() }
}
Route(.case(HouseRoute.create)) {
Method.post
Path { "houses"; }
}
}
You can also specify Method.get
in the first route, but it is not necessary because it is assumed when left off.
from vapor-routing.
Also I am going to close this issue because it is not a bug in the library, and I am going to move it to a discussion so that we can continue discussing if you have more questions.
from vapor-routing.
Related Issues (11)
- ObjectId.parser() instand -> UUID.parser() HOT 1
- How do I run on IOS for SiteRouter -> this target supports 9.0
- How to use Vapor Middleware HOT 3
- Cannot convert value of type 'Dictionary<String, [String]>' to expected argument type 'OrderedDictionary<String, [String?]>' HOT 2
- Longer POST/PUT body fails inconsistently (tests attached) HOT 4
- More detailed JSON Decoding error message HOT 3
- Breaking change in release 0.1.3 HOT 1
- Documentation links aren't working HOT 1
- Vapor Abort doesn't work as expected. HOT 7
- Unable to run with Docker 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-routing.