Home  >  Article  >  Backend Development  >  golang receives get request

golang receives get request

WBOY
WBOYOriginal
2023-05-14 18:23:38712browse

The standard library of the Go language (Golang) provides functions for handling HTTP requests, which makes it very easy to use Go for web applications. In this article, we will discuss how to write an HTTP server using Go to receive GET requests. We will first discuss how to create an HTTP server using Go, and then introduce methods for handling Get requests.

Create Go HTTP Server

The Go language standard library provides a package called "net/http", which provides all the functions needed to create an HTTP server. The basic steps to create an HTTP server in Go language are as follows:

  1. Import the "net/http" package.
  2. Create a function to handle HTTP requests and wrap it in a handler function.
  3. Assign a handler function to the URL path so that the function is triggered when the URL is requested.
  4. Start the HTTP server.

Let's look at a sample code where we will create a simple HTTP server that will use the default port 8000 and listen on localhost for HTTP requests from clients:

    package main

    import (
        "fmt"
        "net/http"
    )

    func handler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Hello, World!")
    }

    func main() {
        http.HandleFunc("/", handler)
        http.ListenAndServe(":8000", nil)
    }

In the above code, we define a handler function handler, which only sends the text "Hello, World!" to the client. We then use the http.HandleFunc function to associate the handler function with the URL path "/". Finally, we start the server using the http.ListenAndServe function and listen on TCP port 8000 on localhost.

Handling GET requests using Go

As with other HTTP methods, handling GET requests requires writing a processor function that will perform the operations required to receive the GET request from the client. The basic steps for handling a GET request are as follows:

  1. Retrieve the parameters in the GET request from the request object.
  2. Perform the required action and generate a response.
  3. Send the response back to the client.

Let's look at the code below, which is an updated version of the handler function that will receive a GET request from the client and return a response containing the query parameters:

    func handler(w http.ResponseWriter, r *http.Request) {
        query := r.URL.Query().Get("query")
        fmt.Fprintln(w, "GET请求,参数为:", query)
    }

In the above code, we retrieve the query parameters of the URL from the http.Request object and assign them to a variable named query. We then use this variable to generate a response where we output the text "GET request with parameters:" and the values ​​of the query parameters.

Send a GET request

To test our server, you can use the curl tool or a web browser to send a GET request. curl is an open source command line tool that can be used to send requests to HTTP servers.

To send a GET request using curl, open a terminal and type the following command:

curl http://localhost:8000/?query=hello

The above command will send a GET request to our server with the query parameter "hello". If everything is fine, you should receive the following response on your terminal:

GET请求,参数为:hello

This response indicates that our HTTP server successfully received the GET request from the client and returned a response with the query parameters.

Conclusion

In this article, we have learned how to use Go language to create an HTTP server, receive GET requests from clients and generate responses. Handling HTTP requests and responses becomes easy using the HTTP packages provided in the standard library. The methods explained in this article should be enough to get you started writing web applications in Go.

The above is the detailed content of golang receives get request. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn