本文整理汇总了Golang中net/url.Values.Add方法的典型用法代码示例。如果您正苦于以下问题:Golang Values.Add方法的具体用法?Golang Values.Add怎么用?Golang Values.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/url.Values
的用法示例。
在下文中一共展示了Values.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Detect_language
func Detect_language(q string) string {
var Url *url.URL
Url, err := url.Parse("https://www.googleapis.com")
if err != nil {
fmt.Println(err)
}
Url.Path += "/language/translate/v2/detect"
parameters := url.Values{}
parameters.Add("q", q)
parameters.Add("key", Google_key())
Url.RawQuery = parameters.Encode()
resp, err := http.Get(Url.String())
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var data gtresponse
json.Unmarshal(body, &data)
lang := data.Data.Detections[0][0].Language
return lang
}
示例2: URL
// URL returns the current working URL.
func (r *Request) URL() *url.URL {
p := r.pathPrefix
if r.namespaceSet && len(r.namespace) > 0 {
p = path.Join(p, "namespaces", r.namespace)
}
if len(r.resource) != 0 {
p = path.Join(p, strings.ToLower(r.resource))
}
// Join trims trailing slashes, so preserve r.pathPrefix's trailing slash for backwards compat if nothing was changed
if len(r.resourceName) != 0 || len(r.subpath) != 0 || len(r.subresource) != 0 {
p = path.Join(p, r.resourceName, r.subresource, r.subpath)
}
finalURL := &url.URL{}
if r.baseURL != nil {
*finalURL = *r.baseURL
}
finalURL.Path = p
query := url.Values{}
for key, values := range r.params {
for _, value := range values {
query.Add(key, value)
}
}
// timeout is handled specially here.
if r.timeout != 0 {
query.Set("timeout", r.timeout.String())
}
finalURL.RawQuery = query.Encode()
return finalURL
}
示例3: copyValues
func copyValues(dst, src url.Values) {
for k, vs := range src {
for _, value := range vs {
dst.Add(k, value)
}
}
}
示例4: buildUrl
/*
Build URL for an API method call. Note that when using GET, we will also append query parameter to URL
*/
func (api *Api) buildUrl(method string, option map[string]string) string {
q := url.Values{}
for k, v := range option {
q.Add(k, v)
}
return api.config.Endpoint(method) + "?" + q.Encode()
}
示例5: Login
func (h *ZJUJudger) Login(_ UserInterface) error {
h.client.Get("http://acm.zju.edu.cn/onlinejudge/login.do")
uv := url.Values{}
uv.Add("handle", h.username)
uv.Add("password", h.userpass)
req, err := http.NewRequest("POST", "http://acm.zju.edu.cn/onlinejudge/login.do", strings.NewReader(uv.Encode()))
if err != nil {
return BadInternet
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := h.client.Do(req)
if err != nil {
log.Println("err", err)
return BadInternet
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
html := string(b)
if strings.Index(html, "Handle or password is invalid.") >= 0 ||
strings.Index(html, "Handle is required.") >= 0 ||
strings.Index(html, "Password is required.") >= 0 {
return LoginFailed
}
return nil
}
示例6: GetBookmarkCounts
// GetBookmarkCounts call hatena bookmark count api.
func GetBookmarkCounts(urls []string) (map[string]int, error) {
query := neturl.Values{}
for _, url := range urls {
u, err := neturl.Parse(url)
if err != nil {
return map[string]int{}, err
}
query.Add("url", u.String())
}
req, _ := neturl.Parse(EntryCountsAPIURL)
req.RawQuery = query.Encode()
res, err := http.Get(req.String())
if err != nil {
return map[string]int{}, err
}
body, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()
counts := map[string]int{}
json.Unmarshal(body, &counts)
return counts, nil
}
示例7: buildBody
func (c *Client) buildBody(params map[string]string) url.Values {
body := url.Values{}
for k := range params {
body.Add(k, params[k])
}
return body
}
示例8: GetMessages
func GetMessages(secret, deviceid string) (mr *MessagesResponse, err error) {
pform := url.Values{}
pform.Add("secret", secret)
pform.Add("device_id", deviceid)
// fmt.Printf("Requesting messages with %s\n", pform.Encode())
resp, err := http.Get("https://api.pushover.net/1/messages.json?" + pform.Encode())
if err != nil {
return
}
if resp.StatusCode == 200 {
var jbuf []byte
jbuf, err = ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read from body: %s", err)
return
}
mr = &MessagesResponse{}
jerr := json.Unmarshal(jbuf, mr)
if jerr != nil {
fmt.Printf("Bad unmarshal: [%s]\n", jerr)
return
}
// fmt.Printf("M is %d %t %+v\n", mr.APIResponse.Status, mr.User.DesktopLicense, *mr)
} else {
b, _ := ioutil.ReadAll(resp.Body)
err = fmt.Errorf("Bad Messages response from pushover: %s\n%s", resp.Status, b)
}
return
}
示例9: Get
func (r *RestService) Get(urlPath string, params map[string]string) ([]byte, int, error) {
loc, _ := url.Parse(r.endpoint)
loc.Path = urlPath
values := url.Values{}
for k, v := range params {
values.Add(k, v)
}
loc.RawQuery = values.Encode()
if os.Getenv("KDEPLOY_DRYRUN") == "1" {
log.Infof("Get request url: %s", loc.String())
return nil, 200, nil
} else {
log.Debugf("Get request url: %s", loc.String())
}
response, err := r.client.Get(loc.String())
if err != nil {
return nil, -1, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, response.StatusCode, fmt.Errorf("unexpected http error code: %v (%s)", response.StatusCode, http.StatusText(response.StatusCode))
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, -1, err
}
return body, response.StatusCode, nil
}
示例10: Update
// Subscribes a customer to a new plan.
//
// see https://stripe.com/docs/api#update_subscription
func (self *SubscriptionClient) Update(customerId string, params *SubscriptionParams) (*Subscription, error) {
values := url.Values{"plan": {params.Plan}}
// set optional parameters
if len(params.Coupon) != 0 {
values.Add("coupon", params.Coupon)
}
if params.Prorate {
values.Add("prorate", "true")
}
if params.TrialEnd != 0 {
values.Add("trial_end", strconv.FormatInt(params.TrialEnd, 10))
}
if params.Quantity != 0 {
values.Add("quantity", strconv.FormatInt(params.Quantity, 10))
}
// attach a new card, if requested
if len(params.Token) != 0 {
values.Add("card", params.Token)
} else if params.Card != nil {
appendCardParamsToValues(params.Card, &values)
}
s := Subscription{}
path := "/v1/customers/" + url.QueryEscape(customerId) + "/subscription"
err := query("POST", path, values, &s)
return &s, err
}
示例11: handleConnectRequest
func handleConnectRequest(form *url.Values, body interface{}) {
_, isConduitConnect := body.(*requests.ConduitConnectRequest)
if isConduitConnect {
form.Add("__conduit__", "true")
}
}
示例12: DeleteTask
func (s *Server) DeleteTask(name string) error {
v := url.Values{}
v.Add("name", name)
req, err := http.NewRequest("DELETE", s.URL()+"/task?"+v.Encode(), nil)
if err != nil {
return err
}
client := &http.Client{}
r, err := client.Do(req)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code got %d exp %d", r.StatusCode, http.StatusOK)
}
// Decode valid response
type resp struct {
Error string `json:"Error"`
}
d := json.NewDecoder(r.Body)
rp := resp{}
d.Decode(&rp)
if rp.Error != "" {
return errors.New(rp.Error)
}
return nil
}
示例13: Send
func (s *MailGunServer) Send(message Message) int {
if Debug {
InfoLog.Printf("sending email from %s to %s with subject %s via MailGun.\n", message.From, message.To, message.Subject)
}
data := url.Values{}
data.Set("from", message.From)
for _, to := range message.To {
data.Add("to", to)
}
data.Set("subject", message.Subject)
data.Set("text", message.Text)
r, err := http.NewRequest("POST", s.Server.Url+"messages", bytes.NewBufferString(data.Encode()))
check(err)
r.SetBasicAuth("api", mailGunKey)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
if Debug {
InfoLog.Println("Sending Request " + r.URL.String())
}
res, err := http.DefaultClient.Do(r)
if err != nil {
ErrorLog.Println("Error sending mail via MailGun", err)
return 500
}
if Debug {
InfoLog.Println("Received: " + res.Status)
}
return res.StatusCode
}
示例14: redeemCode
func (p *OauthProxy) redeemCode(code string) (string, error) {
if code == "" {
return "", errors.New("missing code")
}
params := url.Values{}
params.Add("redirect_uri", p.redirectUrl.String())
params.Add("client_id", p.clientID)
params.Add("client_secret", p.clientSecret)
params.Add("code", code)
params.Add("grant_type", "authorization_code")
log.Printf("body is %s", params.Encode())
req, err := http.NewRequest("POST", p.oauthRedemptionUrl.String(), bytes.NewBufferString(params.Encode()))
if err != nil {
log.Printf("failed building request %s", err.Error())
return "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
json, err := apiRequest(req)
if err != nil {
log.Printf("failed making request %s", err.Error())
return "", err
}
access_token, err := json.Get("access_token").String()
if err != nil {
return "", err
}
return access_token, nil
}
示例15: GetMatches
func (h *httpApplicator) GetMatches(selector labels.Selector, labelType Type) ([]Labeled, error) {
params := url.Values{}
params.Add("selector", selector.String())
params.Add("type", labelType.String())
// Make value copy of URL; don't want to mutate the URL in the struct.
urlToGet := *h.matchesEndpoint
urlToGet.RawQuery = params.Encode()
resp, err := h.client.Get(urlToGet.String())
if err != nil {
return []Labeled{}, err
}
defer resp.Body.Close()
matches := []string{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&matches)
if err != nil {
return []Labeled{}, err
}
labeled := make([]Labeled, len(matches))
for i, s := range matches {
labeled[i] = Labeled{
ID: s,
LabelType: labelType,
Labels: labels.Set{},
}
}
return labeled, nil
}