schema icon indicating copy to clipboard operation
schema copied to clipboard

[question] "+" symbol

Open Eleron8 opened this issue 4 years ago • 4 comments

Describe the problem you're having

A clear and concise description of what the bug is. Hi everyone! I want to parse "PhoneNumber" param in request's query. And I have to use phone number with "+" symbol. But it parses phone number and avoid "+" symbol For example, query is like "....PhoneNumber=+48111222333" and after decoding it shows "48111222333" How can I parse with "+" symbol?

I would be grateful for any help

Thanks) …

Versions

Go version: go version go version go1.17 darwin/arm64 package version: run git rev-parse HEAD inside the repo github.com/gorilla/schema v1.2.0

"Show me the code!"

A minimal code snippet can be useful, otherwise we're left guessing!

type RequestParams struct {
    Name string
    Phone string
}
var query models.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 {
                  return err
      }

Hint: wrap it with backticks to format it

Eleron8 avatar Nov 25 '21 17:11 Eleron8

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)

DavidLarsKetch avatar Jan 20 '22 12:01 DavidLarsKetch

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.

zak905 avatar Mar 19 '22 21:03 zak905

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

zak905 avatar Mar 26 '22 22:03 zak905

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

zak905 avatar Jul 03 '22 19:07 zak905

yes, please close

ptman avatar Mar 15 '24 09:03 ptman

Closing out

jaitaiwan avatar Mar 15 '24 09:03 jaitaiwan