-1

I have a rest service in salesforce which it has to receive different requests. When I test the service sending requests, if I use a REST Client I am able to get the requestbody properly but if I use curl to send the request, always I get an empty request body.

Where is the problem??

My code is so simply

@RestResource(urlMapping='/web/*') //Endpoint
global class MRS_WS_FAMRS_Web {
@HttpPost
global static String doPost(){
    String reqBody;
    try{
        RestRequest req = RestContext.request; 
        //For debug only
        Blob reqBody2 = req.requestBody;
        System.debug('reqBody2: ' + reqBody2);
        System.debug('reqBody2size: ' + reqBody2.size());
        reqBody = req.requestBody.toString();
        System.debug('reqBody: ' + reqBody);
        if(req.requestBody.toString() != null){
            System.debug('REQUEST BODY -->: ' + req.requestBody.toString());
            MRS_FAMRS_MethodUtils.enviarMail(req.requestBody.toString(), 'Cuerpo request');
            return 'MRS_WS_FAMRS_Web --> DATA RECEIVED';
        }else return 'request body is null';
     }catch(Exception e){
            return 'ERROR';
     }
}
}

If I use a rest client I can get the data

enter image description here

But if I use curl I can't

enter image description here

I don't know if I am doing something wrong. Please help!!

Edit: To test with curl I have used curl online, the curl command is:

curl -X POST -k -H 'Authorization: OAuth XXXXX' -i 'https://cs108.salesforce.com/services/apexrest/web' --data '{"status":1,"msg":"","response":[{"id":"1","nombre":"","letra":"C"},{"id":"2","nombre":"Protector","letra":"P"},{"id":"3","nombre":"Estudiante","letra":"E"},{"id":"4","nombre":"Benefactor","letra":"B"}]}'

And the response I get is:

enter image description here

SOLVED: I have added the header Content-type: application/json

1

Check the HTTP response code that are being returned. It could be that you have an error in your curl request.

EDIT: pass your body content enclosed with double quotes instead of single quotes and escape the inner quotes in your JSON.

| improve this answer | |
  • question edit with the curl command and the answer – Javier Feb 19 at 10:35
  • @javier I edited your post, NEVER leave your auth token in there! – POZ Feb 19 at 12:29
  • also, I wouldn't trust a third party site with that info, you should use a local curl. – POZ Feb 19 at 12:30
  • I updated my answer. – POZ Feb 19 at 12:51
  • thanks @POZ i solved the problem adding the header Content-type: application/json. And so many thanks for warning about the token, it was a copy paste code, I was desperated and I didn't realize about it. Thank you again – Javier Feb 19 at 14:36
-1

Solved adding the header Content-type: application/json. Now I am able to get the request body if I send the request via curl

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.