forked from shurcooL/graphql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpMethods.go
40 lines (36 loc) · 1.16 KB
/
httpMethods.go
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
package graphql
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"golang.org/x/net/context/ctxhttp"
)
// GetWithQueryString sends an http get request with the query and variables as a query string
func GetWithQueryString(ctx context.Context, client *http.Client, graphqlURL string, query string, variables map[string]interface{}) (*http.Response, error) {
queryString := url.QueryEscape(query)
variableBytes, err := json.Marshal(variables)
if err != nil {
return &http.Response{}, err
}
variableString := url.QueryEscape(string(variableBytes))
resp, err := ctxhttp.Get(ctx, client, graphqlURL+`?query=`+queryString+`&variables=`+variableString)
return resp, err
}
func PostWithBearerToken(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader, token string) (*http.Response, error) {
var resp *http.Response
req, err := http.NewRequest("POST", url, body)
if err != nil {
return resp, err
}
req.Header.Set("Content-Type", "application/json")
if token != `` {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err = ctxhttp.Do(ctx, client, req)
if err != nil {
return resp, err
}
return resp, nil
}