gorilla / schema

Package gorilla/schema fills a struct with form values.
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
1.37k stars 227 forks source link

[question] "+" symbol #185

Closed Eleron8 closed 6 months ago

Eleron8 commented 2 years ago

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

DavidLarsKetch commented 2 years 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)
zak905 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

ptman commented 6 months ago

yes, please close

jaitaiwan commented 6 months ago

Closing out