You have to authenticate before you can call the rest resource. You authentication is what allows you to access the endpoint
A little while ago, Paul McGurn blogged how to create a public
(SOAP-based) web service via a Force.com Site. As Paul explains, you
add your web service class to the Enabled Apex Classes in the Site’s
Public Access Settings. You’ll also need to add the appropriate object
and field-level permissions for any data you will be exposing;
remembering, of course, that this web service will be accessible
without any authentication!
As I was setting up a WebHook the other day, I realized that the same
principle applies to Apex REST Methods – add the class and any
relevant objects/fields to your Site’s Public Access Settings, and you
can access the REST methods via the Site URL. Let’s code up a simple
example; here’s my Apex class:
https://developer.salesforce.com/blogs/developer-relations/2012/02/quick-tip-public-restful-web-services-on-force-com-sites.html
Oh Got the solution. I think I need to spend more time with my research before asking question here.
Here is the working code. Actually after adding content type in header(also JSON.stringify()). It works
Below is my working code
function createSobjectRecord() {
var accountInfo = {
"Name": "Testing Jquery with Rest"
};
var accountInfoJson = JSON.stringify(accountInfo);
$j.ajax({
type: "POST",
url: "/services/data/v35.0/sobjects/Account",
headers: {
'Authorization': "OAuth " + sessionId,
'Content-Type': 'application/json'
},
crossDomain: true,
data: JSON.stringify(accountInfo),
dataType: 'json',
success: function(responseData, status, xhr) {
console.log(responseData);
},
error: function(request, status, error) {
console.log(request.responseText);
}
});
Complete code to help other may be it will helpful for someone.
GET, POST AND DELETE Method using Jquery
<apex:page>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div>
<input class="btn" value="Get Account list" type="button" onclick="getSobjectList()" />
<input class="btn" value="Create Account Record" type="button" onclick="createSobjectRecord()" />
<input class="btn" value="Delete Account Record" type="button" onclick="deleteSobjectRecord()" />
</div>
<div id="warehouseList">
</div>
<script type="text/javascript">
var $j = jQuery.noConflict();
var sessionId = '{!$Api.Session_ID}';
function getSobjectList() {
$j.ajax({
type: "GET",
url: "/services/data/v35.0/query?q="+''+"{!URLENCODE('SELECT Id, Name FROM Account')}",
headers: {"Authorization" : "OAuth " + sessionId},
crossDomain : true,
dataType: 'application/json',
success: function (responseData) {
console.log(responseData);
},
error: function (request, status, error) {
console.log(request.responseText);
}
});
}
var recordId;
function createSobjectRecord() {
var accountInfo = { "Name" :"Testing Jquery with Rest"};
var accountInfoJson = JSON.stringify(accountInfo);
$j.ajax({
type: "POST",
url: "/services/data/v35.0/sobjects/Account",
headers : {
'Authorization' : "OAuth " + sessionId,
'Content-Type' : 'application/json'
},
crossDomain : true,
data: JSON.stringify(accountInfo),
dataType: 'json',
success: function (responseData, status, xhr) {
console.log(responseData.id);
recordId = responseData.id;
},
error: function (request, status, error) {
console.log(request.responseText);
}
});
}
function deleteSobjectRecord(){
$j.ajax({
type: "Delete",
url: "/services/data/v35.0/sobjects/Account/"+recordId,
headers : {
'Authorization' : "OAuth " + sessionId,
'Content-Type' : 'application/json'
},
crossDomain : true,
success: function (responseData, status, xhr) {
console.log(responseData);
},
error: function (request, status, error) {
console.log(request.responseText);
}
});
}
</script>
</apex:page>
Best Answer
The standard REST API doesn't support the PUT method.
(Source: Understanding Force.com REST Resources)
Depending on what you are trying to achieve you can use POST or PATCH.
To create a new record then you can use POST. For example to create a new Account you can POST to
/services/data/v36.0/sobjects/Account
To update an existing record you can use PATCH. For example to update an existing Account you can PATCH to
/services/data/v36.0/sobjects/Account/YOUR_ACCOUNT_ID