2

[Golang] Get Query String of URL in net/http Handler

 2 years ago
source link: https://siongui.github.io/2017/03/24/go-get-url-query-string-in-http-handler/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Golang] Get Query String of URL in net/http Handler

March 24, 2017

Get query string of URL in request handler of HTTP server via Go standard net/http Package.

Question:

Assume the URL of the HTTP request is

https://example.com/?name=john

I want to get the value of name, i.e., john from the URL. How to get it in HTTP request handler of net/http?

Answer:

import (
      "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
      name := r.URL.Query().Get("name")
      // the value of name is john
}

For more information about how to access the query string, see net/url package.


Tested on:

  • Ubuntu Linux 16.10
  • Go 1.8

References:

[2][Golang] JSONP Server Implementation Code[3]How to make a secured user authentication and authorization system? What are the things I should look for? : golang[4]Structure of API project, i.e.: Where should what part of the code be? : golang[5]GoBuffalo and Authboss (authentication system) integration : golang[6]Create Self-Signed Certs and Pin them with Go : golang


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK