Home > Backend Development > Golang > How to Effectively Terminate HTTP Requests in Go?

How to Effectively Terminate HTTP Requests in Go?

Susan Sarandon
Release: 2024-11-25 08:18:13
Original
785 people have browsed it

How to Effectively Terminate HTTP Requests in Go?

Terminating HTTP Requests in Go

In Go, you can terminate an HTTP request by returning from the ServeHTTP() method of an HTTP handler. This approach allows you to end the request and send a response to the client.

Example:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // Examine incoming parameters
    if !ok {
        str := `{"Result":"","Error":"No valid Var"}`
        fmt.Fprint(w, str)
        return // Terminate the request
    }

    // Continue normal API serving
})
Copy after login

Notes:

  • Consider returning an HTTP error code instead of the default 200 OK when your API's input parameters are invalid. Use the http.Error() function for this purpose.

Example:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // Examine incoming parameters
    if !ok {
        http.Error(w, `Invalid input params!`, http.StatusBadRequest) 
        return // Terminate the request
    }

    // Continue normal API serving
})
Copy after login
  • For more control, you can set headers and return JSON error responses explicitly.

Example:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // Examine incoming parameters
    if !ok {
        w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusBadRequest)
        str := `{"Result":"","Error":"No valid Var"}`
        fmt.Fprint(w, str)
        return // Terminate the request
    }

    // Continue normal API serving
})
Copy after login

Propagation of Errors:

If you encounter an error outside of ServeHTTP(), you must return that error state so that ServeHTTP() can terminate the request.

Example:

type params struct {
    // Field definitions for your parameters
}

func decodeParams(r *http.Request) (*params, error) {
    p := new(params)
    // Decode parameters
    if !ok {
        return nil, errors.New("Invalid params")
    }
    return p, nil
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    p, err := decodeParams(r)
    if err != nil {
        http.Error(w, `Invalid input params!`, http.StatusBadRequest)
        return // Terminate the request
    }

    // Continue normal API serving
})
Copy after login

The above is the detailed content of How to Effectively Terminate HTTP Requests in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template