+ Start a Discussion

How to create a httpPut method in apex rest

i want to create a method (via PUT) of the Apex REST web service  to receive a list of string and also the code to invoke it from java.


actually am looking for sample code!

the documentation doesnt have such examples.


I want a method like say

public static void doPut(list<String> listObj){}

 i can declare it in apex but i want to know how to invoke this from java?


Sankalp JhingranSankalp Jhingran



You can invoke the REST service that you have created in Apex by creating a new HTTPPut object in java and using the your REST service URL that you have defined in your REST service.


Here is an example:


public void RestMethod1(){
HttpClient httpclient = new DefaultHttpClient();
System.out.println("Creating HTTP Client");

HttpPut put = new HttpPut("https://instance.salesforce.com/services/apexrest/Account/<sample account id>");
HttpResponse response = null;

// set the token in the header
System.out.println("Setting authorization header");
get.setHeader("Authorization", "OAuth 00D50000000IoZ6!AQYAQJ4Q.w9jmFMZJNRlw54y4RuUxfQYGGJzebS3LFL2xEgmvUaWs7xhS6OJrkcO5jw2zPR1I3xhuiNVVXWaiKU_7iibsZ8J");

System.out.println("Executing Put request");
response = httpclient.execute(put);
HttpEntity entity = response.getEntity();

}catch(Exception e){


The URL specified when creating the HTTPPut is what you have defined in the:

@RestResource(urlMapping='/Account/*') at the top of your REST class in APEX along with the standard REST URL for salesforce like https://instance.salesforce.com/services/apexrest


Hope it is helpful.




Blog: forcesecrets.blogspot.com


Thanks but I am looking to invoke external services from within salesforce.

Sankalp JhingranSankalp Jhingran



Your original issue said, you wanted to create a REST service in apex and then invoke it from Java, now you are saying you want to invoke external services from within salesforce. Can you please clarify:


Nevertheless, you can invoke any external webservice or even an web service created in apex rest like this:


Http h = new Http();
HttpRequest req1 = new HttpRequest();
req1.setEndpoint('<end point of your REST service>');
HttpResponse res1 = null;
res1 = h.send(req1);
system.debug('%%%%%%%%%%%%%%%%%%%%%% ' + res1.getBody());







Let me know ,if that helps you  .


HttpPut: Defines the function to be used with the endpoint with HTTP PUT POST


      global static String updateCase(RestRequest req) {
          String companyName = req.params.get('companyName');
          Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName];
          Attachment a = new Attachment();
          a.ParentId = company.Id;
              a.Name = 'test.png';
          a.Body = req.requestBody;
          insert a;
          return 'Attachment added';


Kind Regards,

Mayank Joshi



can i use @HttpPut with two global static methods in same rest class?

I am getting error like "@HttpPut already used elsewhere in class".

How to resolve this?

farukh sk hdfarukh sk hd
Hope this will help you.

Here rest api is used to update record in second salesforce system from first.