Golang HTTP Request Handling

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab aims to test your ability to use the net/http package in Golang to issue HTTP requests.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/NetworkingGroup(["`Networking`"]) go/NetworkingGroup -.-> go/http_client("`HTTP Client`") subgraph Lab Skills go/http_client -.-> lab-15481{{"`Golang HTTP Request Handling`"}} end

HTTP Client

You are required to write a program that sends an HTTP GET request to a server and prints the HTTP response status and the first 5 lines of the response body.

  • The program should use the net/http package to issue an HTTP GET request.
  • The program should print the HTTP response status.
  • The program should print the first 5 lines of the response body.
  • The program should handle errors gracefully.
$ go run http-clients.go
Response status: 200 OK
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example</title>

There is the full code below:

// The Go standard library comes with excellent support
// for HTTP clients and servers in the `net/http`
// package. In this example we'll use it to issue simple
// HTTP requests.
package main

import (
	"bufio"
	"fmt"
	"net/http"
)

func main() {

	// Issue an HTTP GET request to a server. `http.Get` is a
	// convenient shortcut around creating an `http.Client`
	// object and calling its `Get` method; it uses the
	// `http.DefaultClient` object which has useful default
	// settings.
	resp, err := http.Get("https://gobyexample.com")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	// Print the HTTP response status.
	fmt.Println("Response status:", resp.Status)

	// Print the first 5 lines of the response body.
	scanner := bufio.NewScanner(resp.Body)
	for i := 0; scanner.Scan() && i < 5; i++ {
		fmt.Println(scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		panic(err)
	}
}

Summary

In this lab, you have learned how to use the net/http package in Golang to issue HTTP requests and handle errors.

Other Go Tutorials you may like