Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very peculiar :HTTP Status 405 - Method Not Allowed

[using Apache Tomcat/7.0.27]

It seems I only get this error

  • (HTTP Status 405 - Method Not Allowed)

when I try to make a REST request directly from the browser.

E.g by pasting this in the address bar :

http://localhost:8080/restExample/rest/catalog/video/14951/hello

When I run my test client Main.java everything works fine.

Any ideas as to why it wont let me execute a REST through the browser?

Client Side:

public class Main{
    public static void main(String [] args){
       ClientConfig config = new DefaultClientConfig();
       Client client = Client.create(config);   
       WebResource service = client.resource(getBaseURI(_package));
       runPutRequest(service,"video/128/This is the content with the new description");
    }
}

...
private static void runPutRequest(WebResource service,String path){
        String response = service.path("rest/catalog/"+path).accept(MediaType.APPLICATION_XML).put(String.class);
        System.out.println("Post Response :"+response);
    }

Server side:

@PUT
@Path("/video/{video-id}/{short-descr}")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_XML)
public Video updateVideo(@PathParam("video-id") int contentid,
                         @PathParam("short-descr") String descr)
{       
    //Video video = searchByContentId(contentid);
    Video video = videoMap.get(contentid);
    video.setDescription(descr);

    videoMap.put(contentid,video);
    
    if( videoMap.get(contentid) != null){
        return videoMap.get(contentid);
    }else{
         throw new UnsupportedOperationException("NO object found");
    }
}
like image 202
Fabii Avatar asked May 18 '12 20:05

Fabii


People also ask

What is a 405 code?

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

How do I fix Nginx Error 405 Not allowed?

Check the Requested URL The most common cause of a 405 Method Not Allowed is simply inputting an incorrect URL. As discussed before, many web servers will disallow access to improper URLs. This could be anything from trying to access a file directory via a URL to gaining access to a private page meant for other users.

How do I create a 405 error?

HTTP response status code 405 means Method Not Allowed . This status code states that HTTP method was received and recognized by the server, but the server has rejected that particular method for the requested resource. Try to access an existing resource without the proper permission.


2 Answers

The browser will issue a GET request for your resource - which you have declared as a @PUT on the server-side and are PUT-ing to it from your client-side code. The browser is trying to 'fetch' (or GET) the resource and nothing exists for @GET

like image 78
PhD Avatar answered Oct 02 '22 18:10

PhD


Generally, the Browser uses GET HTTP method to make requests. Your server side component is only capable to response to PUT requests, and that’s why you get that error code.

like image 42
Carlos Gavidia-Calderon Avatar answered Oct 02 '22 20:10

Carlos Gavidia-Calderon