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.
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)
return
}
log.Printf("%q
", dump)
var city string = r.FormValue("city")
log.Printf("city: %q
", city)
}