Giter VIP home page Giter VIP logo

Comments (6)

iampatbrown avatar iampatbrown commented on June 14, 2024 1

@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.

anhar avatar anhar commented on June 14, 2024

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.

anhar avatar anhar commented on June 14, 2024

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:
bug

This code snippet sets the method property to nil somewhere down the callstack:

Route(.case(HouseRoute.create)) {
    Method.post
    Path { "houses"; }
}

See screenshot here:
bug2

from vapor-routing.

iampatbrown avatar iampatbrown commented on June 14, 2024

@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.

mbrandonw avatar mbrandonw commented on June 14, 2024

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.

mbrandonw avatar mbrandonw commented on June 14, 2024

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)

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.