import r "github.com/solos/requests"

func translationPHandler(w http.ResponseWriter,rr *http.Request){ if rr.Method == "POST" { con := rr.PostFormValue("con") lang := rr.PostFormValue("lang") fmt.Println(con, lang) kwargs := r.M{} options := r.M{ "timeout": 10, } // 设定请求头 headers := map[string]string{ "content-Type": "application/json", } // 设定POST请求数据 这里写的post请求,在我的python服务上接收不到数据(主要问题再python服务,不在go的请求方法上),这种方式传值是传过去的值是json,不是query data := map[string]string{ "con": con, "lang": lang, } // 实例化一个请求对象 req := &r.Request{Args: kwargs} //tansUrl := "http://localhost:6002/baiduTrans?con="+con+"&lang="+lang tansUrl := "http://localhost:6002/baiduTrans" // POST登录 resp, err := req.MakeRequest("POST", tansUrl, r.Headers(headers), r.Options(options), r.Data(data)) //resp, err := req.MakeRequest("POST", tansUrl, r.Headers(headers), r.Options(options)) if err != nil { err := common.Error{ Code: -1, Error:"err", } fmt.Print(2) encodeInfo,_ :=json.Marshal(&err) fmt.Fprint(w,string(encodeInfo)) }else { //fmt.Print(body) //fmt.Fprint(w, string(body)) fmt.Fprint(w, resp.Content) } }else{ err := common.Error{ Code: -1, Error:"Method Not Allowed", } encodeInfo,_ :=json.Marshal(&err) fmt.Fprint(w,string(encodeInfo)) } }

要想使用  import r "github.com/solos/requests",需要先下载对应的依赖,下载方式为:

go get github.com/solos/requests

之后使用上述方法,请求第三方接口就可以了。

借鉴于:https://github.com/solos/requests