Comments (19)
From the example, createUser handler doesn't accept param.
from echo.
I've modified the handlers like so:
e.Get("/users", getUsers)
e.Get("/users/:id", getUser)
e.Post("/users", createUser)
e.Post("/users/:name", createUser)
but now get a Method Not Allowed
. If I comment out the /users/:id
route, it works.
from echo.
Can you paste the full code and how you invoke the endpoint?
from echo.
https://gist.github.com/e168fc8fe9be985901fd
from echo.
e.Get("/users/:id", getUser)
e.Post("/users/:name", createUser)
With current implementation, above is registered as conflicting routes, so if you change :name to :id, it should work. Let me know your thoughts.
Conflicting routes are captured in #3
from echo.
Is it possible to do this without a pretty URL? Just /users?name=joel
?
Since
e.Get("/users/:id", getUser)
e.Post("/users/:name", createUser)
should not be conflicting routes.
from echo.
Based on this example https://golang.org/pkg/net/url/#example_URL
c.Request.URL.Query().Get("user")
from echo.
@vishr I'm not sure what you are trying to say.
I meant that in most other frameworks, those two routes are distinct.
I unfortunately still get an empty string.
from echo.
https://gist.github.com/471fb336acb72f02d726
from echo.
Just realized, it won't work. I will work on fixing the implementation to accommodate this scenario.
from echo.
Including the duplication of routes?
from echo.
Duplicate routes like below will be covered in #3
e.Get("users/:id")
e.Get("users/:id")
e.Get("users/:name")
from echo.
Now I just cannot connect to port 8080 (with the same code).
from echo.
I tried your original code https://gist.github.com/e168fc8fe9be985901fd for all the registered routes. Output below:
➜ tmp curl -X GET localhost:8080/users
{"1":{"id":"1","name":"Wreck-It Ralph"}}
➜ tmp curl -X GET localhost:8080/users/1
{"id":"1","name":"Wreck-It Ralph"}
➜ tmp curl -X POST localhost:8080/users
{"id":"2","name":""}
➜ tmp curl -X POST localhost:8080/users/Joe
{"id":"3","name":"Joe"}
Did you update Echo to latest?
from echo.
I thought I did update, but I could be wrong. How should I go about updating?
from echo.
http://labstack.github.io/echo/guide/#installation
from echo.
Alright, I've upgraded. With the same code, though, I get an empty string when supplying data like so:
curl -X POST --data "{'name':'maxwell123'}" localhost:8081/users
Perhaps I am doing this wrong.
from echo.
@tekknolagi I see in your curl command you are not sending Content-Type header, context.Bind currently only supports JSON. I am not sure but looks like JSON also needs to be in double quotes. Below is the working example
package main
import (
"net/http"
"strconv"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
)
type user struct {
ID string `json:"id"`
Name string `json:"name"`
}
var users map[string]user
var nextid int
func init() {
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
}
nextid = 2
}
func nextUserId() string {
var next = nextid
nextid++
return strconv.Itoa(next)
}
func createUser(c *echo.Context) error {
u := new(user)
if err := c.Bind(u); err != nil {
return err
}
u.ID = nextUserId()
users[u.ID] = *u
return c.JSON(http.StatusCreated, u)
}
func getUsers(c *echo.Context) {
c.JSON(http.StatusOK, users)
}
func getUser(c *echo.Context) {
c.JSON(http.StatusOK, users[c.P(0)])
}
func main() {
e := echo.New()
e.Use(mw.Logger)
//--------
// Routes
//--------
e.Get("/users", getUsers)
e.Get("/users/:id", getUser)
e.Post("/users", createUser)
e.Post("/users/:name", createUser)
// Start server
e.Run(":8081")
}
curl -X POST -H 'Content-Type: application/json' --data '{"name":"maxwell123"}' localhost:8081/users
{"id":"2","name":"maxwell123"}
from echo.
Ah, okay. This looks like it should work now. Thank you!
from echo.
Related Issues (20)
- `Host` header always blank HOT 6
- Does echo support i18n? [feature request] HOT 2
- Weirdness with wrapped handler and embed FS (static files) HOT 1
- Remove default charset from 'application/json' Content-Type header
- echo v4 websocket client closed, program exited HOT 3
- router not setting ContextKeyHeaderAllow for group, causing cors preflight to not work HOT 6
- Static file has wrong body returned when using a custom HTTP Error Handler and complex paths HOT 2
- QueryParams seem to override formValues HOT 3
- DefaultHTTPErrorHandler does not log HOT 3
- Support Casbin Authorization Middleware HOT 1
- Echo response object calls flush on unflushable objects HOT 7
- Feature request: support Yaml responses transparently. HOT 1
- static middleware: path ist unescaped twice for file names, leading to not downloadable content
- CSRF middleware form lookup consumes all the request body HOT 1
- Question: How can I serve content from static HTML files using URLs that have no '.html' extension HOT 5
- Omitempty in form HOT 1
- Return error if reverse() does not find a match HOT 9
- How to read a path param with echo that can contain slashes? HOT 3
- Unclear behaviour of `*` in routes HOT 7
- middleware.CORSConfig can not be configured per path 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 echo.