FAQ
I've written a bunch of Go servers, but they all expect the client to POST
a request to me. If someone called my server with

http://myserver/script?key1=val1&key2=val2&key3=val3........ etc. how do I
get the key/value pairs into Go?
Thanks,
Ken

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Egon at Nov 20, 2015 at 6:54 pm

    On Friday, 20 November 2015 20:38:55 UTC+2, Ken MacDonald wrote:
    I've written a bunch of Go servers, but they all expect the client to POST
    a request to me. If someone called my server with

    http://myserver/script?key1=val1&key2=val2&key3=val3........ etc. how do
    I get the key/value pairs into Go?
    Thanks,
    Ken
    They are called "query parameters" search for them
    in https://golang.org/pkg/net/http

    One way to access POST, PUT body and query params at the same time is:

    var r *http.Request
    err := r.ParseFrom()
    // check for err, if needed

    fmt.Println(r.FormValue("key1"))
    fmt.Println(r.Form)

    + Egon

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrio Almeida at Nov 21, 2015 at 12:08 am
    You can switch between GET and POST actions too.


    func (this *myHandler) LoginHandler(w http.ResponseWriter, r *http.Request) {


         switch r.Method {


         ///////////////////////////////////////////////////

         case "GET": //case GET method


             //do something....


         ///////////////////////////////////////////////////


         case "POST": //case POST method

             _email := r.FormValue("email")

             _pass := r.FormValue("pass")


             log.Print(_email)

             log.Print(_pass)

         }

    }



    Em sexta-feira, 20 de novembro de 2015 15:38:55 UTC-3, Ken MacDonald
    escreveu:
    I've written a bunch of Go servers, but they all expect the client to POST
    a request to me. If someone called my server with

    http://myserver/script?key1=val1&key2=val2&key3=val3........ etc. how do
    I get the key/value pairs into Go?
    Thanks,
    Ken
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 20, '15 at 6:38p
activeNov 21, '15 at 12:08a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase