GO - net / http - how to get the query string and method

Using net / http, I want to get the current query string and method, i.e. GET, POST, PUT, etc.

Is it possible - I do not see it in the documents

+4
source share
3 answers

You can use the following fields of the query structure:

Method string

and

RequestURI string

I suggest you look at the struct source code: Request the project source code

You can access it by clicking the structure name in the go document for the net / http package.

+7
source

In the docs, start at http://golang.org/pkg/net/http and follow the "Type Request" link at http://golang.org/pkg/net/http#Request . All you need is available as a field or query method.

For example, the HTTP method is Request.Method. The path and query parameters are in the request .URL.Path and Request.URL.Query () respectively.

 http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s %q", r.Method, html.EscapeString(r.URL.Path)) }) log.Fatal(http.ListenAndServe(":8080", nil)) 
+5
source

Yes, you can. all this data is available inside HttpRequest. If you look in Request.HttpMethod, you will see a method (i.e. Get). You also have access to header information, if necessary. depending on what you are doing, FilePath , Path and several other properties will provide you with the data that was published.

0
source

Source: https://habr.com/ru/post/1494152/


All Articles