Coder Social home page Coder Social logo

mux headers do not support regex about mux HOT 11 CLOSED

gorilla avatar gorilla commented on May 2, 2024
mux headers do not support regex

from mux.

Comments (11)

jmassara avatar jmassara commented on May 2, 2024

This would be really useful. See here. It would be nice to do something like this:

r.HandleFunc("/myapp", MyHandler).
    Methods("GET").
    Headers("Accept", "application/vnd.myapp-v{version:[0-9]+}+json")
http.Handle("/", r)

Or:

r.HandleFunc("/myapp", MyHandler).
    Methods("GET").
    Headers("Accept", "application/vnd.myapp-v{version:[0-9]+}+{format:json|xml}")
http.Handle("/", r)

from mux.

fdelbos avatar fdelbos commented on May 2, 2024

+1

this:

r.HandleFunc("/path", MyHandler).
        Methods("POST").
        Headers("Content-Type", "application/json")

will not match a request with "Content-Type", "application/json;charset=UTF-8"

and this:

r.HandleFunc("/path", MyHandler).
        Methods("POST").
        Headers("Content-Type", "application/json;charset=UTF-8")

will not match a request with "Content-Type", "application/json"

from mux.

eastwood avatar eastwood commented on May 2, 2024

I've added regex support. An example of usage is as follows:

    r := mux.NewRouter()
    r.HandleFunc("/", RootHandler).Headers("Content-Type", "application/json.*")
    http.ListenAndServe(":8080", r)
}

Now both requests will work:

curl -X GET -H "Content-Type: application/json" localhost:8080
curl -X GET -H "Content-Type: application/json;charset=UTF-8" localhost:8080

My personal uses were for handling application/(json|text)

from mux.

bluehallu avatar bluehallu commented on May 2, 2024

@eastwood isn't HeadersRegexp what should be used here?

from mux.

eastwood avatar eastwood commented on May 2, 2024

@bluehallu That's correct. Above comment is out of date. The documentation should be up to date with this, if anybody requires it

from mux.

bluehallu avatar bluehallu commented on May 2, 2024

@eastwood I've tested the code and it behaves as you'd expect, but the docs are in sync with your comment above:

"...Alternatively, you can provide a regular expression and match the header as follows..."

from mux.

eastwood avatar eastwood commented on May 2, 2024

Ah I see :) Good pickup, sorry about the confusion, must of been a legacy commit. I'll clean that up right away to fix the confusion. Alternatively, you can fix the change and make a pull request, the more contributors the merrier!

from mux.

eastwood avatar eastwood commented on May 2, 2024

#125 Should fix this up for us

from mux.

bluehallu avatar bluehallu commented on May 2, 2024

I wanted to make sure I wasn't misunderstanding this before doing a PR. All
fixed now so thanks :)

On Tue, 8 Sep 2015 12:34 Clinton Ryan [email protected] wrote:

#125 #125 Should fix this up for us


Reply to this email directly or view it on GitHub
#16 (comment).

from mux.

jmassara avatar jmassara commented on May 2, 2024

It would be nice to support capturing regex matches in mux.Vars() (see my previous comment), similiar to how the router.HandlerFunc does it.

from mux.

kisielk avatar kisielk commented on May 2, 2024

@jmassara please open a separate issue for that.

from mux.

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.