Skip to content

Commit e88e77a

Browse files
authored
Merge pull request #2 from zry98/master
Fix InvokeRequest
2 parents 055ea72 + e258ff6 commit e88e77a

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

requests.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,33 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"strings"
89
)
910

1011
type Body struct {
1112
// Ok: if true, request was successful, and result can be found in the Result field.
1213
// If false, error can be explained in Error field.
1314
Ok bool `json:"ok"`
14-
// Error: contains a human readable description of the error result.
15+
// Error: contains a human-readable description of the error result.
1516
Error string `json:"error"`
1617
// Result: result of requests (if Ok)
1718
Result json.RawMessage `json:"result"`
1819
}
1920

2021
func InvokeRequest(method string, params url.Values) (json.RawMessage, error) {
21-
r, err := http.NewRequest(http.MethodPost, "https://api.telegra.ph/"+method, nil)
22+
r, err := http.NewRequest(http.MethodPost, "https://api.telegra.ph/"+method, strings.NewReader(params.Encode()))
2223
if err != nil {
23-
return nil, fmt.Errorf("failed to build GET request to %s: %w", method, err)
24+
return nil, fmt.Errorf("failed to build POST request to %s: %w", method, err)
2425
}
25-
r.URL.RawQuery = params.Encode()
2626
resp, err := http.DefaultClient.Do(r)
2727
if err != nil {
28-
return nil, fmt.Errorf("failed to execute GET request to %s: %w", method, err)
28+
return nil, fmt.Errorf("failed to execute POST request to %s: %w", method, err)
2929
}
3030
defer resp.Body.Close()
3131

3232
var b Body
33-
3433
if err = json.NewDecoder(resp.Body).Decode(&b); err != nil {
35-
return nil, fmt.Errorf("failed to decode GET request to %s: %w", method, err)
34+
return nil, fmt.Errorf("failed to parse response from %s: %w", method, err)
3635
}
3736
if !b.Ok {
3837
return nil, fmt.Errorf("failed to %s: %s", method, b.Error)

0 commit comments

Comments
 (0)