Golang HTTP Client: Get, Post, Timeout, Header, Cookie Example

Golang HTTP Client Example


golang http clientThe Go net/http package includes several methods for talking to HTTP services. Considering that HTTP requests are a fundamental part included in many of today’s applications, this article will focus on several examples.

The Go HTTP Client can be used a variety of ways depending on your requirements. Using the minimal http.Get functionality can work for quick programs but you may find that you need more features, such as setting timeout values or adding headers.

Overview

Headers

The net/http package has many functions that handle headers, those include the Add, Del, Get and Set methods. While Add appends to any existing values associated with key, Set replaces any existing values associated with key.

Minimal Get Example

This minimal example uses http.Get from the net/http standard library package. It reads the entire response body to a variable named body and then prints it to standard output:

Extended Get Example

This example demonstrates how to set the client request timeout and headers. More on timeout can be found in this article here.

Post Example

This example demonstrates how to post.

See also:
Golang SSH Client
Go Command Line Flags
Go IP Address Manipulation
Golang Regular Expression Match