Closed Eleron8 closed 6 months ago
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)
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.
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
@elithrar I think this can be closed, as the issue is not relevant to schema
yes, please close
Closing out
Describe the problem you're having
I would be grateful for any help
Thanks) …
Versions
"Show me the code!"
…