function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
snejanasnejana 

Problems with creating new object via REST

Hi all, please, help.

 

I need to use REST for creating new portal user
I use apigee.com.
I create class and GET works well, but I have problems with POST.

 

@RestResource(urlMapping='/WSPortalUser/*')
global with sharing class WSPortalUser{

    @HttpGet
    global static User doGet(){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String userId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        User result = [SELECT Id, Name FROM User WHERE Id = :userId];
        return result;
    }
  
    @HttpPost
    global static String doPost(String name){
    	return name;
    }
}

 GET:

https://na14.salesforce.com/services/apexrest/WSPortalUser/005i0000000M2lB

HTTP/1.1 200 OK
Date:
Thu, 13 Jun 2013 02:44:33 GMT
Content-Length:
144
Content-Type:
application/json;charset=UTF-8

{
  "attributes":  {
    "type": "User",
    "url": "/services/data/v24.0/sobjects/User/005i0000000M2lBAAS"
  },
  "Name": "Admin",
  "Id": "005i0000000M2lBAAS"
}

 POST:

https://na14.salesforce.com/services/apexrest/WSPortalUser?name=TestName

HTTP/1.1 400 Bad Request
Date:
Thu, 13 Jun 2013 02:53:18 GMT
Content-Length:
118
Content-Type:
application/json;charset=UTF-8

[
   {
    "message": "The HTTP entity body is required, but this request has no entity body.",
    "errorCode": "JSON_PARSER_ERROR"
  }
]

 What do I do wrong? Please, any ideas.

Best Answer chosen by Admin (Salesforce Developers) 
snejanasnejana

I found the solve my problem:

1. Wrong url, should be

https://na14.salesforce.com/services/apexrest/WSPortalUser/

2. I must set into header

content-type:application/json

3. I must set into body the following text

{
  "name": "TestName"
}

Problem occurred because of the absence in the apigee.com the 'apexrest' API method.

So, I should click on the 'POST Create Object' first of all and then the 'Body' tab was appeared.

Sorry for bother.

 

All Answers

snejanasnejana

I found the solve my problem:

1. Wrong url, should be

https://na14.salesforce.com/services/apexrest/WSPortalUser/

2. I must set into header

content-type:application/json

3. I must set into body the following text

{
  "name": "TestName"
}

Problem occurred because of the absence in the apigee.com the 'apexrest' API method.

So, I should click on the 'POST Create Object' first of all and then the 'Body' tab was appeared.

Sorry for bother.

 

This was selected as the best answer
yogeshweryogeshwer

is this  work for cross platform.???

snejanasnejana
yogeshwer, what do you mean?
yogeshweryogeshwer
i saying that it will work for Cross platform there is any need of set-up
for that. server site require any webservice to handle the request.
Regards
yogeshwer
Nishad BashaNishad Basha
{
        "message": "Unexpected parameter encountered during deserialization: name at [line:3, column:12]",
        "errorCode": "JSON_PARSER_ERROR"
    }                    plz   i    got  this error plz help me the that line