

[Golang] HTTP Request With Custom User-Agent
source link: http://siongui.github.io/2018/02/27/go-http-request-with-custom-user-agent/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Golang] HTTP Request With Custom User-Agent
February 27, 2018
Example of HTTP request with custom User-Agent header via Go standard net/http package.
package main import ( "errors" "io/ioutil" "net/http" "strconv" ) func HTTPRequestCustomUserAgent(url, userAgent string) (b []byte, err error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return } req.Header.Set("User-Agent", userAgent) client := &http.Client{} resp, err := client.Do(req) if err != nil { return } defer resp.Body.Close() if resp.StatusCode != 200 { err = errors.New( "resp.StatusCode: " + strconv.Itoa(resp.StatusCode)) return } return ioutil.ReadAll(resp.Body) } func main() { b, err := HTTPRequestCustomUserAgent("https://www.google.com/", "My User-Agent") if err != nil { panic(err) } println(string(b)) }
If you want to know how to send HTTP request with cookies, please see [2].
Tested on: Ubuntu Linux 17.10, Go 1.10.
References:
[1]GitHub - siongui/instago: Get photos, videos, stories, following and followers of Instagram
Recommend
-
72
问题及场景 业务当中有需要分发http.request.body的场景。比如微信回调消息只能指定一个地址,所以期望可以复制一份消息发给其他服务。由服务B和接收微信回调的服务A一起处理微信回调信息。 解决思路 最...
-
18
How to add User-Agent header to HttpClient in .NET Written by Kristoffer Strube, December 15, 2020 An intrinsic part of the modern web application is...
-
8
So we have this list of Web sites in Spain which are not completely working properly with
-
7
如何更改url package访问HTTP时的user-agent header 有些网站会根据 http request 中的 user-agent header 的值返回不同的response,例如 http://wttr.in 会根据就会根据 user-agent 是否为 curl 来决定是返回带图片的...
-
7
【Root-Me】 HTTP 水题,打开挑战页面后提示:Wrong user-agent: you are not the "admin" browser! 。
-
2
[Golang] HTTP Request With Cookies March 03, 2018 Example of sending HTTP reques...
-
7
Introduction Access HTTP Request Header in Golang code running on your browser. This can be done by
-
19
[Golang] Parse Accept-Language in HTTP Request Header February 22, 2015 This post i...
-
8
How to make HTTP Get Request using Golang and Rust? Published: Apr 4, 2021 / Last modified: Apr 4, 2021 This code snippet is part of my local repository for demo a...
-
7
golang复用http-request-body | PPmoon's Blog golang复用http-request-body Posted on 2018-10-20 in golang •...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK