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
Kaushik Muhuri 15Kaushik Muhuri 15 

http request

Is there any specific reason for the following error:
Status=Length Required, StatusCode=411
I am using http post in apex.
Agustin BAgustin B
Hi Kaushik, you need to specify Content-Length in the header of the request.
something like req.setHeader('Content-Length', '4096'); But it depends on the length of your request
Hope it helps, if it does please mark as correct, it may help others
Jatin Gupta 21Jatin Gupta 21
Hi Agustin, Since the body is blank in my request, I have set the content-length to '0'.Does that help ?
Agustin BAgustin B
Yes you can.
If i am being helpful please like or mark as correct, if not please send full request but that should do it
Jatin Gupta 21Jatin Gupta 21
Here is the request:
string url = 'https://............./';
        string key = '*************************';
        string mobile = '**********';
        string userid = '********';
//        string start_date = System.now().addHours(-1).format('dd-MMM-yyyy HH:MM:ss').replace(' ','%20'); 
        string start_date = '14-MAY-2020%2015:00';
      System.debug('===start_date==='+start_date);
//        string to_date = System.now().format('dd-MMM-yyyy HH:MM:ss').replace(' ','%20');
      string to_date = '14-MAY-2020%2017:00';
        System.debug('===to_date==='+to_date);
        string endpoint = 'GLUSR_MOBILE/'+mobile +'/GLUSR_MOBILE_KEY/'+key+'/Start_Time/'+start_date+'/End_Time/'+to_date+'/';
        System.debug('>>>> endpoint >>>>>' + endpoint);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader( 'Content-Type', 'application/json;charset=utf-8' );
        request.setEndpoint(url+endpoint);
        request.setMethod('POST');
        request.setHeader('Content-Length', '0');
        System.debug('>>>> Request>>>>>' + request);
        HttpResponse response = http.send(request);
        
Agustin BAgustin B
Hi Jatin, but are you getting the same error with request.setHeader('Content-Length', '0');?

Another question, the service you are trying to get has a "get" endpoint that you could try?
Jatin Gupta 21Jatin Gupta 21
Hi Agustin,Yes I am getting the same error 
Agustin BAgustin B
Hi Jatin, I need more information, I am trying to help you.
The content lenght in 0 should have fixed this mistake, you could try values other than 0 but the error should not be 411.
The other question would be, does this api have a get call you could try instead of this post?

Please mark the first comment as correct as it is the answer for this question, I have no problem to keep answering you but this is solved with that solution
Jatin Gupta 21Jatin Gupta 21
Hello All,
I am experiencing a new problem.The same endpoint when run from Postman gives expected result.
But with Apex HTTP Request I am getting : Status=Service Unavailable, StatusCode=503.
I know that there is no chances of the service being unavailable.
Agustin BAgustin B
Hi Jatin, check that the url you are trying to fetch is enabled on Setup -> Security -> Remote Site Settings.
If that is not the case then the server may be down for maintenance but if the url and everything is right on your side the issue is on the other end.

That is what the 503 error means, the only thing from your end could be the remote site settings.

Good luck, and if it helps please put as correct or give like to the answers that helped you. It is encouragement for us that use our time to help on this community.
David Zhu 🔥David Zhu 🔥
I dont' see you set body for POST request.
If body is empty, try add:

request.setBody('{}'); 

if  still not working, try add:

request.setHeader('Content-Length', '0');