doukangbin9698
doukangbin9698
2017-11-01 03:53

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

  • rest
  • java
  • http
  • httpclient

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.

Request:

_url = new URL(_urlBase+method);
_http = (HttpURLConnection) _url.openConnection();
_http.setRequestMethod(requestType);
_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.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐