Created
March 5, 2018 12:00
-
-
Save slav123/9e3efdbbce03cf78c12257f882fd1687 to your computer and use it in GitHub Desktop.
send data via POST golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"net/url" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
apiUrl := "https://api.com" | |
resource := "/user/" | |
data := url.Values{} | |
data.Set("name", "foo") | |
data.Add("surname", "bar") | |
u, _ := url.ParseRequestURI(apiUrl) | |
u.Path = resource | |
urlStr := u.String() // "https://api.com/user/" | |
client := &http.Client{} | |
r, _ := http.NewRequest("POST", urlStr, strings.NewReader(data.Encode()) // <-- URL-encoded payload | |
r.Header.Add("Authorization", "auth_token=\"XXXXXXX\"") | |
r.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) | |
resp, _ := client.Do(r) | |
fmt.Println(resp.Status) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment