Sending HTTP Request with Golang

Last renew: April 7, 2022 pm

The way to send a HTTP request with golang

注:本文仅为个人学习笔记,无任何版权。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
"encoding/base64"

"github.com/imroc/req/v3"
"github.com/jltorresm/otpgo"
"github.com/jltorresm/otpgo/config"
)


func main() {
t := otpgo.TOTP{
Key: "wang.tianchen333@gmail.comHENNGECHALLENGE003",
Algorithm: config.HmacSHA512,
}
token, _ := t.Generate()
token1 := "wang.tianchen333@gmail.com" +":" + token
auto := "basic " + base64.StdEncoding.EncodeToString([]byte(token1))

user := &User{github: "https://gist.github.com/xiaogeamadeus/ad9d74723a99a882c850eabb66c51d9d",
contact: "wang.tianchen333@gmail.com",
solution: "golang"}
client := req.C().EnableDumpAllWithoutResponse()
client.R().
SetHeaders("Authorization", auto,
"Content-Type", "application/json",).
SetBody(user).
Post("https://api.challenge.hennge.com/challenges/003")

}

type User struct {
github string `json:"github_url"`
contact string `json:"contact_email"`
solution string `json:"solution_language"`
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2022/03/12 20:53:40.763848 DEBUG [req] GET https://api.challenge.hennge.com/challenges/003
:authority: api.challenge.hennge.com
:method: GET
:path: /challenges/003
:scheme: https
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
accept-encoding: gzip

:status: 403
content-type: application/json
content-length: 68
date: Sat, 12 Mar 2022 11:53:42 GMT
x-amzn-requestid: 2492fed6-b665-4d3e-becb-0532ed6fb218
access-control-allow-origin: https://challenge.hennge.com
access-control-allow-headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token
x-amzn-errortype: MissingAuthenticationTokenException
x-amz-apigw-id: O3p3EH04NjMFdIQ=
access-control-allow-methods: GET,OPTIONS,POST,PUT
x-cache: Error from cloudfront
via: 1.1 e2f427863e6bdb72ad8bed72b596d81e.cloudfront.net (CloudFront)
x-amz-cf-pop: OSL50-P1
x-amz-cf-id: nLWbqmaTIGAh2btKEQOFDkk5LDTgu6DBtboJS9a-OCPfaEZ08ubPqQ==

{
"message": "User is not authorized to access this resource"
}
1
"github_url": "https://gist.github.com/xiaogeamadeus/ad9d74723a99a882c850eabb66c51d9d","contact_email": "wang.tianchen333@gmail.com","solution_language": "golang"
1
{"github_url":"https://gist.github.com/hennge/b859bd12e7a7fb418141","contact_email":"ninja@example.com","solution_language":"golang"}