-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhttpresp.go
More file actions
107 lines (87 loc) · 2.3 KB
/
httpresp.go
File metadata and controls
107 lines (87 loc) · 2.3 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package instago
// This file sends HTTP requests and gets HTTP responses.
import (
"errors"
"io/ioutil"
"net/http"
"strconv"
)
// Send HTTP request and get http response without login and with gis info. Used
// in get all post codes without login.
func getHTTPResponseNoLoginWithGis(url, gis string) (b []byte, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
req.Header.Set("User-Agent", appUserAgent)
req.Header.Set("X-Requested-With", "XMLHttpRequest")
req.Header.Set("X-Instagram-GIS", gis)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = errors.New(url +
"\nresp.StatusCode: " + strconv.Itoa(resp.StatusCode))
return
}
return ioutil.ReadAll(resp.Body)
}
// Send HTTP request and get http response without login.
func GetHTTPResponseNoLogin(url, method string) (b []byte, err error) {
if method != "POST" {
method = "GET"
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return
}
req.Header.Set("User-Agent", appUserAgent)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = errors.New(url +
"\nresp.StatusCode: " + strconv.Itoa(resp.StatusCode))
return
}
return ioutil.ReadAll(resp.Body)
}
// Send HTTP request and get http response on behalf of a specific Instagram
// user. After login to Instagram, you can get the cookies of *ds_user_id*,
// *sessionid*, *csrftoken* in Chrome Developer Tools.
// See https://stackoverflow.com/a/44773079
// or
// https://github.com/hoschiCZ/instastories-backup#obtain-cookies
func (m *IGApiManager) getHTTPResponse(url, method string) (b []byte, err error) {
if method != "POST" {
method = "GET"
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return
}
for name, value := range m.cookies {
req.AddCookie(&http.Cookie{Name: name, Value: value})
}
for name, value := range m.headers {
req.Header.Set(name, value)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = errors.New(url +
"\nresp.StatusCode: " + strconv.Itoa(resp.StatusCode))
return
}
return ioutil.ReadAll(resp.Body)
}