doulongti5932 2014-11-22 04:56
浏览 59
已采纳

在Go中请求URL

I like to ask, if how can i run URL using go. I have ruby code that i like to convert to Go.

url4 = "https://rest.nexmo.com/sms/xml?api_key=KEY&api_secret=SECRET&from=Aphelion&to=#{params[:user][:mobile_num]}&text=Test SMS"
encoded_url3 = URI.encode(url4)
url5= URI.parse(encoded_url3)
req3 = Net::HTTP::Get.new(url5.to_s)
res = Net::HTTP.start('rest.nexmo.com', 80) {|http|
  http.request(req3)
}

Thank you

  • 写回答

1条回答 默认 最新

  • dsk88199 2014-11-22 06:00
    关注

    The standard net/http package provides a default http client for performing http requests.

    package main
    
    import (
            "fmt"
            "io/ioutil"
            "net/http"
    )
    
    func main() {
            resp, err := http.Get("https://rest.nexmo.com/sms/xml")
            if err != nil {
                    panic(err)
            }
            defer resp.Body.Close()
            body, err := ioutil.ReadAll(resp.Body)
            if err != nil {
                    panic(err)
            }
            fmt.Printf("%s", body)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?