+ Start a Discussion
Elizabete FonsecaElizabete Fonseca 

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

Hi there!

I'm trying to test a apexrest api that I just built, but I can't seem to get past this error:
[{"message":"The HTTP entity body is required, but this request has no entity body.","errorCode":"JSON_PARSER_ERROR"}]

What exactly does this mean "The HTTP entity body is required, but this request has no entity body."? And how can I set the entity body in the request?

I've looked through almost all of the similar issues I found online and tried the solutions, but still nothing worked for me.

I'm using CURL to make this request:
curl -X POST 'https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&...' -H 'Content-Type: application/json' -H 'Content-Length: 0' -H 'Accept-type: application/json' -H 'Authorization:Bearer myaccesstoken'

And the apexrest code is:
@RestResource(urlMapping='/v1/MyRestApi/*')
global class MyRestApi {

	@HttpPost 
	global static void doPost(String param1, ...){
		
        map<String, Object> JSONResponseMap = (...);
        JSON.serialize(JSONResponseMap);        
        RestContext.response.responseBody = Blob.valueOf(JSON.serialize(JSONResponseMap));
		
	} 	  
}
If anyone has any idea of what's missing or what I am doing wrong, I would greatly appreciate some guidance!

Thanks in advance!
EF


 
Manish BhatiManish Bhati
There are two ways :-
- If you don't want to send data in request then use 
curl -X POST https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&param2=value2

- If you want to send data in the request then use below:-
curl -X POST -d '{"title":"Hello World!","body":"This is my first post!"}' https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&... "Content-Type:application/json"

Also, if you like to only change in your curl cmd only then try with some data :-
curl -X POST -d '{"title":"Hello World!","body":"This is my first post!"}' https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&... -H 'Content-Type: application/json' -H 'Content-Length: 0' -H 'Accept-type: application/json' -H 'Authorization:Bearer myaccesstoken'

Hope this helps.
Elizabete FonsecaElizabete Fonseca

Hi Manish Bhati, 

Thanks so much for responding! 

I tried your suggestions all of your suggestions but I got errors in all of them:

1st - about the MISSING Content-Type and Length;
2nd - about the INVALID SESSION ID;
3rd - same error about the entitty body required.

Any other ideas why is this could be?

Thanks a million for your assistance!

Cheers,
EF

Manish BhatiManish Bhati
First try this :-
 
$ curl https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&param2=value2 -d '{"title":"Hello World!","body":"This is my first post!"}' -H 'Content-Type: application/json'
{"id":"1","title":"Hello World!","body":"This is my first post!"}

Then we can figure out the problem.

Also, let me know whether in method doPost you are creating request or not.
Elizabete FonsecaElizabete Fonseca

I tried as you suggested and I get this error: 
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

because there's no access_token. 

I the doPost method I do not create the request, but rather the response... is that what you are asking?

Thanks for your assistance :)

Cheers,
EF

Manish BhatiManish Bhati
My Bad forgot the Authorization part:-

Try 1:-
curl https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&param2=value2 -H "Authorization: Bearer myaccesstoken" -H "Content-Type: application/json" -d {"id":"1","title":"Hello World!","body":"This is my first post!"}
or if you could save json file in the folder where you are running this curl cmd from:-
- Save a file named newaccount.json
- with the below data
{
    "Name" : "test"
}

- run the below curl cmd from where the file newaccount.json reside, crosscheck the file is not newaccount.json.txt if you create from text doc.
 
curl https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&param2=value2 -H "Authorization: Bearer myaccesstoken" -H "Content-Type: application/json" -d "@newaccount.json"

 
Elizabete FonsecaElizabete Fonseca
Hi Manish Bhati!

I apologize for leaving you hanging yesterday... but some other stuff came up and I had to prioritize. 

I tried the 2 ways you suggested and both worked! 

I believe the reason why I was having problems was because of the " (double quotes) / ' (single quotes) situation, which can be tricky to figure out depending on the machine you are using.

I use Linux, so for anyone that might encounter this in the future, it seems that everything needs to be enclosed in single quotes: 
curl 'https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&param2=value2' -H 'Authorization: Bearer myaccesstoken' -H 'Content-Type: application/json' -d '{"id":"1","title":"Hello World!","body":"This is my first post!"}'
or 
curl 'https://myinstance.salesforce.com/services/apexrest/v1/MyRestApi?param1=value1&param2=value2' -H 'Authorization: Bearer myaccesstoken' -H 'Content-Type: application/json' -d '@newaccount.json'

Once again, Manish, thank you so much for your assistance! 

Best regards,
EF