Giter Club home page Giter Club logo

Comments (6)

zak905 avatar zak905 commented on May 28, 2024 1

Hi @Eleron8,

it's likely that this has nothing to do with the schema package as @DavidLarsKetch pointed out. The + sign is interpreted as an escape sign in a http query, as a space to be more precise, and therefore when ?phone=+48111222333 is sent, the value received after url decoding is 48111222333 (is there a trailing space ?). Not sure which kind of client you have, I think you need to replace the + with a %2B or do a url encoding before sending.

from schema.

zak905 avatar zak905 commented on May 28, 2024 1

Here is a full example:

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println(r.URL.Query().Get("phone"))
	})

	log.Fatal(http.ListenAndServe(":9090", nil))

}

trying curl "localhost:9090?phone=+12456789"

the printed message is: 12456789

This issue should be closed as not related directly to schema project, but rather to query parameters encoding

from schema.

zak905 avatar zak905 commented on May 28, 2024 1

@elithrar I think this can be closed, as the issue is not relevant to schema

from schema.

DavidLarsKetch avatar DavidLarsKetch commented on May 28, 2024

Hey @Eleron8, I'm not able to reproduce the issue with the above code snippet. Here's what I did:

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strings"

	"github.com/gorilla/schema"
)

type RequestParams struct {
	Name  string
	Phone string
}

func main() {
        name := "gorilla-schema"
        phone := "+123456789"
        
	r := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
	q := r.URL.Query()
	q.Add("name", name)
	q.Add("phone", phone)
	r.URL.RawQuery = q.Encode()

	var query RequestParams
	decoder := schema.NewDecoder()
	decoder.RegisterConverter([]string{}, func(input string) reflect.Value {
		return reflect.ValueOf(strings.Split(input, ","))
	})
	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
		fmt.Println(err)
	}
	fmt.Printf("name: expected %s, got %s, they match: %t\n", name, query.Name, name==query.Name)
	fmt.Printf("phone: expected %s, got %s, they match: %t\n", phone, query.Phone, phone==query.Phone)

from schema.

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.