Coder Social home page Coder Social logo

Param returning empty string about echo HOT 19 CLOSED

labstack avatar labstack commented on May 3, 2024
Param returning empty string

from echo.

Comments (19)

vishr avatar vishr commented on May 3, 2024

From the example, createUser handler doesn't accept param.

from echo.

tekknolagi avatar tekknolagi commented on May 3, 2024

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.

vishr avatar vishr commented on May 3, 2024

Can you paste the full code and how you invoke the endpoint?

from echo.

tekknolagi avatar tekknolagi commented on May 3, 2024

https://gist.github.com/e168fc8fe9be985901fd

from echo.

vishr avatar vishr commented on May 3, 2024
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.

tekknolagi avatar tekknolagi commented on May 3, 2024

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.

vishr avatar vishr commented on May 3, 2024

Based on this example https://golang.org/pkg/net/url/#example_URL

c.Request.URL.Query().Get("user")

from echo.

tekknolagi avatar tekknolagi commented on May 3, 2024

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

tekknolagi avatar tekknolagi commented on May 3, 2024

https://gist.github.com/471fb336acb72f02d726

from echo.

vishr avatar vishr commented on May 3, 2024

Just realized, it won't work. I will work on fixing the implementation to accommodate this scenario.

from echo.

tekknolagi avatar tekknolagi commented on May 3, 2024

Including the duplication of routes?

from echo.

vishr avatar vishr commented on May 3, 2024

Duplicate routes like below will be covered in #3

e.Get("users/:id")
e.Get("users/:id")
e.Get("users/:name")

from echo.

tekknolagi avatar tekknolagi commented on May 3, 2024

Now I just cannot connect to port 8080 (with the same code).

from echo.

vishr avatar vishr commented on May 3, 2024

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.

tekknolagi avatar tekknolagi commented on May 3, 2024

I thought I did update, but I could be wrong. How should I go about updating?

from echo.

vishr avatar vishr commented on May 3, 2024

http://labstack.github.io/echo/guide/#installation

from echo.

tekknolagi avatar tekknolagi commented on May 3, 2024

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.

vishr avatar vishr commented on May 3, 2024

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

tekknolagi avatar tekknolagi commented on May 3, 2024

Ah, okay. This looks like it should work now. Thank you!

from echo.

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.