Created
September 25, 2024 22:27
-
-
Save davidpvilaca/523bcf6f4fc6157eecd6680ee9584980 to your computer and use it in GitHub Desktop.
Raw HTTP client with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"net" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
conn, err := net.Dial("tcp", "23.21.73.249:80") | |
if err != nil { | |
fmt.Println("Error dialing", err.Error()) | |
os.Exit(1) | |
} | |
request := "GET /get HTTP/1.1\r\nHost: httpbin.org\r\n\r\n" | |
_, err = conn.Write([]byte(request)) | |
if err != nil { | |
fmt.Println("Error writing", err.Error()) | |
os.Exit(1) | |
} | |
reader := bufio.NewReader(conn) | |
statusLine, err := reader.ReadString('\n') | |
if err != nil { | |
fmt.Println("Error reading", err.Error()) | |
os.Exit(1) | |
} | |
fmt.Println("----------------------------------------------------") | |
fmt.Println("Status Line") | |
fmt.Println("----------------------------------------------------") | |
fmt.Println(statusLine) | |
fmt.Println("----------------------------------------------------\r\n*") | |
headers := make(map[string]string) | |
for { | |
line, err := reader.ReadString('\n') | |
if err != nil { | |
fmt.Println("Erro ao ler os headers:", err) | |
os.Exit(1) | |
} | |
// Se a linha estiver vazia, significa que acabamos de ler os headers | |
if line == "\r\n" { | |
break | |
} | |
// Dividindo o header em chave e valor | |
headerParts := strings.SplitN(line, ": ", 2) | |
if len(headerParts) == 2 { | |
key := strings.TrimSpace(headerParts[0]) | |
value := strings.TrimSpace(headerParts[1]) | |
headers[key] = value | |
} | |
} | |
// Exibindo os headers | |
fmt.Println("----------------------------------------------------") | |
fmt.Println("Headers recebidos") | |
fmt.Println("----------------------------------------------------") | |
for k, v := range headers { | |
fmt.Printf("%s: %s\r\n", k, v) | |
} | |
fmt.Println("----------------------------------------------------\r\n*") | |
contentLength, err := strconv.Atoi(headers["Content-Length"]) | |
if err != nil { | |
fmt.Println("Erro no Content-Length:", err) | |
os.Exit(1) | |
} | |
// Se o Content-Length não foi encontrado, finalizamos o programa | |
if contentLength == 0 { | |
fmt.Println("Content-Length não encontrado ou é 0.") | |
os.Exit(0) | |
} | |
body := make([]byte, contentLength) | |
// Lendo o corpo da resposta | |
_, err = reader.Read(body) | |
if err != nil { | |
fmt.Println("Erro ao ler o corpo da resposta:", err) | |
os.Exit(1) | |
} | |
fmt.Println("----------------------------------------------------") | |
fmt.Println("Corpo da resposta") | |
fmt.Println("----------------------------------------------------") | |
fmt.Println(string(body)) | |
fmt.Println("----------------------------------------------------") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment