logo
down
shadow

What Response Code to return on a non-supported HTTP Method on REST?


What Response Code to return on a non-supported HTTP Method on REST?

By : hanibahuy
Date : November 22 2020, 10:31 AM
This might help you You can set a custom NotFoundHandler but that will apply to all unmatched routes.
If you want a specific response returned you have to define the route explicitly.
code :
func main() {
    r := mux.NewRouter().PathPrefix("/api").Subrouter()

    // custom not found handler used for unmatched routes
    var notFound NotFound
    r.NotFoundHandler = notFound

    r.HandleFunc("/users", APIUsers).Methods("GET", "POST")

    // return 405 for PUT, PATCH and DELETE
    r.HandleFunc("/users", status(405, "GET", "POST")).Methods("PUT", "PATCH", "DELETE")

    http.Handle("/", r)

    http.ListenAndServe(":8083", nil)
}

type NotFound func(w http.ResponseWriter, req *http.Request)

func (NotFound) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    w.WriteHeader(404)
    w.Write([]byte(`{"message": "Not Found"}`))
}

// status is used to set a specific status code
func status(code int, allow ...string) func(w http.ResponseWriter, req *http.Request) {
    return func(w http.ResponseWriter, req *http.Request) {
        w.WriteHeader(code)
        if len(allow) > 0 {
            w.Write([]byte(`Allow: ` + strings.Join(allow, ", ")))
        }
    }
}

func APIUsers(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("hello"))
}


Share : facebook icon twitter icon
what http response code for rest service on put method when domain rules invalid

what http response code for rest service on put method when domain rules invalid


By : user1734745
Date : March 29 2020, 07:55 AM
Hope that helps The response code is not related to the http method in this case. You should return the same status code as if it had been a POST request. I'd say you should use 400 or 409 (Note: See further discussion of the difference between the two in the comments).
Is there any situation the REST service's http response code is 200, but the method returns false

Is there any situation the REST service's http response code is 200, but the method returns false


By : ฐิติกร วงศ์ลังกา
Date : March 29 2020, 07:55 AM
should help you out These methods like deleteObject only return true or an exception when something is wrong or if it does not success. In other words, there is no any situation to return false
What Http method have I to use to modify this REST resource and what response code have I to return?

What Http method have I to use to modify this REST resource and what response code have I to return?


By : RobWJ
Date : March 29 2020, 07:55 AM
this will help I am pretty new in RESTful web services and I have the following doubt. I am working on a Spring MVC application (but this is not so important because my doubt is more related to REST concept). , As a general rule, CRUD operations should follow this pattern:
code :
POST: Create
PUT: Update
GET: Read
DELETE: Delete
Spring Rest return a JSON response with a certain http response code

Spring Rest return a JSON response with a certain http response code


By : kanita
Date : March 29 2020, 07:55 AM
should help you out How I do it
Here is how I do JSON returns from a Spring Handler method. My techniques are somewhat out-of-date, but are still reasonable.
code :
<bean name="jsonView"
    class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
protected ResponseEntity<ResponseJson> buildResponse(
    final ResponseJson jsonResponseBody,
    final HttpStatus httpStatus)
{
    final ResponseEntity<ResponseJson> returnValue;

    if ((jsonResponseBody != null) &&
        (httpStatus != null))
    {
        returnValue = new ResponseEntity<>(
            jsonResponseBody,
            httpStatus);
    }

    return returnValue;
}
@RequestMapping(value = "/webServiceUri", method = RequestMethod.POST)
@PostMethod("/webServiceUri")
public ResponseEntity<ResponseJson> handlerMethod(
    ... params)
{
    ... stuff

    return buildResponse(json, httpStatus);
}
public ResponseEntity<ResponseJson> handlerMethod(
    final WebRequest webRequest,
    @RequestBody final InputJson inputJson)
{
    ... stuff
}
HTTP response always return response code 200 even request fail, and return status code is part of REST

HTTP response always return response code 200 even request fail, and return status code is part of REST


By : user2909863
Date : March 29 2020, 07:55 AM
To fix this issue I've joined a project that uses exactly the same strategy -- embed status message inside the response body, and leave status code to be always 200. For consistency reason, it is better to follow existed strategy during the software maintenance time. However, it is not recommended for any new project, with reasons listed below:
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org