2017-11-01 03:53
浏览 121

Java客户端-Go Http Mux服务器-go方法上的参数为空

I'v got a GO Lang REST API that works normally when I call it from Postman. But, when I try to call a DELETE method with HttpURLConnection parameters are not received by my method.


_url = new URL(_urlBase+method);
_http = (HttpURLConnection) _url.openConnection();
_http.setRequestProperty("Accept", "application/json");    
if ((requestType.toUpperCase().equals("POST")) || (requestType.toUpperCase().equals("DELETE"))) 
    _http.setRequestProperty("Accept", "application/json"); 

    byte[] postData = params.getBytes( StandardCharsets.UTF_8 );
    int    postDataLength = postData.length;

    _http.setDoOutput( true );
    _http.setInstanceFollowRedirects( false );
    _http.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
    _http.setRequestProperty( "charset", "utf-8");
    _http.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
    _http.setUseCaches( false );

    try( DataOutputStream wr = new DataOutputStream( _http.getOutputStream())) {
        wr.write( postData );

if (_http.getResponseCode() != 200) {
     throw new RuntimeException("Failed : HTTP error code : " + _http.getResponseCode());

PS: I've got a three REST APIs in test at this moment. This method just don't pass parameters for GO Lang, using MUX. On Scala and Rails is working fine.

PS: On Go I'm getting parameter from http.Request.FormValue(field).

Go Routes:

r := mux.NewRouter()
r.HandleFunc("/Rest/get", get).Methods("GET")
r.HandleFunc("/Rest/putCity/{city}", putCity).Methods("PUT")
r.HandleFunc("/Rest/updateCity", updateCity).Methods("POST")
r.HandleFunc("/Rest/deleteCity", deleteCity).Methods("DELETE")
http.ListenAndServe(":12345", r)

Method getting parameter:

func deleteCity(w http.ResponseWriter, r *http.Request) {   
    var city string = r.FormValue("city")

Thanks for trying to help guys! Regards.

图片转代码服务由CSDN问答提供 功能建议

我有一个GO Lang REST API,当我从Postman调用它时,它可以正常工作。 但是,当我尝试使用 HttpURLConnection 参数调用 DELETE 方法时,我的方法未收到这些参数。


  _url =新网址(_urlBase +方法); 
_http =(HttpURLConnection)_url.openConnection(); 
_http.setRequestProperty(“ Accept”,  “应用程序/ JSON”);  
if((requestType.toUpperCase()。equals(“ POST”))||(requestType.toUpperCase()。equals(“ DELETE”)))
 _http.setRequestProperty(“ Accept”,“ application /  JSON“);  
 byte [] postData = params.getBytes(StandardCharsets.UTF_8); 
 int postDataLength = postData.length; 
 _http  .setRequestProperty(“ Content-Type”,“ application / x-www-form-urlencoded”);  
 _http.setRequestProperty(“ charset”,“ utf-8”); 
 _http.setRequestProperty(“ Content-Length”,Integer.toString(postDataLength)); 
  try(DataOutputStream wr = new DataOutputStream(_http.getOutputStream())){
if(_http.getResponseCode()!= 200){
抛出新的 RuntimeException(“ Failed:HTTP错误代码:” + _http.getResponseCode()); 

PS:我正在测试三个REST API 在这一刻。 此方法只是不使用 MUX 为GO Lang传递参数。

PS:在运行中,我从 http.Request.FormValue(field)获取参数。 \ n


  r:= mux.NewRouter()
r.HandleFunc(“ / Rest / get”,get).Methods(“ GET  “)
r.HandleFunc(” / Rest / putCity / {city}“,putCity).Methods(” PUT“)
r.HandleFunc(” / Rest / updateCity“,updateCity).Methods(” POST“)
r  .HandleFunc(“ / Rest / deleteCity”,deleteCity).Methods(“ DELETE”)

方法获取 参数:

  func deleteCity(w http.ResponseWriter,r * http.Request){
 var city string = r.FormValue(“ city”)

感谢您的帮助! 问候。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutangguali32556 2017-11-01 08:12

    Looking at the implementation of golang's http.Request.ParseForm(), it seems that the form data is not parsed for DELETE requests.

    I'd suggest using the identifier of the city to be deleted as request parameter in the URL:

    r.HandleFunc("/Rest/deleteCity/{city}", deleteCity).Methods("DELETE")

    While you're at it, you could also refactor the whole definition of the routes to be more consistent with standard REST API design patterns, e.g.,

    r.HandleFunc("/Rest/cities/", getCities).Methods("GET")
    r.HandleFunc("/Rest/cities/{city}", getCity).Methods("GET")
    r.HandleFunc("/Rest/cities/{city}", createCity).Methods("POST") // with body
    r.HandleFunc("/Rest/cities/{city}", updateCity).Methods("PUT") // with body
    r.HandleFunc("/Rest/cities/{city}", deleteCity).Methods("DELETE")

    You can use the following snippet to inspect the request. Please note that this should only used for testing purposes and not in a production environment:

    func deleteCity(w http.ResponseWriter, r *http.Request) {
        log.Println("deleteCity handler called")
        dump, err := httputil.DumpRequest(r, true)
        if err != nil {
            http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
    ", dump)
        var city string = r.FormValue("city")
        log.Printf("city: %q
    ", city)
    打赏 评论

相关推荐 更多相似问题