1

I created Rest api for the User details with Get and PATCH Methods. I am able Access Get method.But for PATCH METHOD it is giving 405 METHOD NOT Allowed.I added headers for accessing the All Methods.

@RestResource(urlMapping='/UserDetails/*')
global class UserDetails
{
    @HttpGet
    global static List<User> getUserDetails() 
    {

        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        res.addHeader('Access-Control-Allow-Origin','*');
        res.addHeader('Cache-Control','no-cache');
        res.addHeader('Access-Control-Max-Age','1728000');
        res.addHeader('Accept', '*');
        res.addHeader('Content-Type','application/json');
        res.addHeader('Accept','application/json');
        res.addHeader('Access-Control-Allow-Methods','GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS');
        res.addHeader('Access-Control-Allow-Headers','Content-Type, Accept');

        //String ID = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        String Id= RestContext.request.params.get('UserId');

        List<User> Users= [SELECT Id, Name, Username, CommunityNickname FROM User where Id=:Id];

        return Users;
    }

    @HttpPatch
    global static String updateUser(List<InputData> request)
    {
         try 
         {
             if(!(request.Size() > 0))
                 throw new CustomException('Input should not be empty'); 

            for(InputData data : request) 
            {
                User UserDetail= [SELECT Id,FirstName,LastName,Username FROM User where Id=:data.userId];

                UserDetail.FirstName=data.FirstName;
                UserDetail.LastName=data.LastName;  

                Update UserDetail;
            }

            return 'User details Updated';
       }
       catch(Exception e) 
       {
           return e.getMessage();
       }
    }

    global Class InputData
    {
        webService String userId { get; set; } 
        webService String FirstName { get; set; }
        webService String LastName { get; set; }
    }

    public Class CustomException extends Exception {}  
}

I am getting Responce as Raw Response

HTTP/1.1 405 Method Not Allowed

Date: Mon, 07 Mar 2016 17:35:28 GMT

Content-Security-Policy-Report-Only: default-src https:; script-src https: 'unsafe-inline' 'unsafe-eval'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /_/ContentDomainCSPNoAuth?type=mydomain Set-Cookie: BrowserId=GHdQQhb3TfO5IwEJ0mw1og;Path=/; Domain=.salesforce.com;Expires=Fri, 06-May-2016 17:35:28 GMT

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Allow: GET,HEAD

Content-Type: application/json;charset=UTF-8

Transfer-Encoding: chunked

[ { "errorCode" : "METHOD_NOT_ALLOWED", "message" : "HTTP Method 'PATCH' not allowed. Allowed are GET,HEAD" } ]

Can any one suggest me where i am doing wrong in this API. It is very urgent.Please Help me

  • how you are calling your method ? – Himanshu Mar 7 '16 at 18:32
  • In workbench i am selecting the PATCH method and running this api it is giving this error. I try to send response as {"request" :[{"UserId":"005L0000002iC3J", "FirstName":"Sssss", "LastName":"uuuu" }]} – Amar Mar 7 '16 at 19:09
  • We developed HTML Native app for our client. In html we are running all api's for fletching the data.Previously all api's getting this Allow Cross Origin issue.After adding the Headers all are working. In this api it is allowing get method so we are able to login.But we are not able to Update the User details. When we try to update this it is giving XMLHttpRequest cannot load sfvud-varian.cs8.force.com/MPP/services/apexrest/UserDetails. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. – Amar Mar 7 '16 at 19:15
  • Origin '209.160.65.49:1095' is therefore not allowed access. The response had HTTP status code 405. This is the error i am getting – Amar Mar 7 '16 at 19:15
  • HI Himanshu can u please suggest me how to give access for the Patch – Amar Mar 7 '16 at 19:21

Browse other questions tagged or ask your own question.