Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Get client’s user agent from an incoming HTTP request in Go (Golang)

Posted on December 18, 2023December 18, 2023 by admin

Table of Contents

  • Overview
  • Example

Overview

User-agent in an incoming HTTP request is present in the headers of the request. In Go an incoming HTTP request is represented by the http.Request struct

https://golang.org/src/net/http/request.go

The http.Request struct exposes the below method for getting the user-agent of the request

func (r *Request) UserAgent() string

This method needs to be called on the request object.

Example

Let’s see the program for the same.

package main
import (
"fmt"
"net/http"
)
func main() {
handler := http.HandlerFunc(handleRequest)
http.Handle("/example", handler)
http.ListenAndServe(":8080", nil)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
userAgent := r.UserAgent()
fmt.Printf("UserAgent:: %s", userAgent)
}

Run the above program and make the below API call. Also pass in the user-agent header

curl -v -X POST http://localhost:8080/example -H "user-agent: Mozialla -1.0"

Output

UserAgent:: Mozialla -1.0

In the above program, we are calling the UesrAgent() function on the request struct object and then printing it. It correctly prints the user agent

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP