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
Jatin Gupta 21Jatin Gupta 21 

HTTP Response 400

I am using HTTP POST for submittig data to an external application.
With Postman the request gives Resonse as expected with HTTP response code of 200.
But while doing the same with apex class and HTTP Request/Response the response is Bad Request, StatusCode=400.
I have tried with different content-type but the error persists.Can anybody help me out ?
 
Best Answer chosen by Jatin Gupta 21
David Zhu 🔥David Zhu 🔥
I notice there are space in these two parameters
  string start_date = '2020-04-01 17:00';    // space between 01 and 17.
string to_date = System.now().format('YYYY-MM-dd HH:mm');   //space between dd and HH

You will haven to encode the parameters before appending the endpoint or try using %20 to replace the space. 

All Answers

Marzorati ClaudioMarzorati Claudio
Hi Jatin,

Bad Request 400 means that your request isn't weel formatted.
Postman enrich the request with full information, althougth in Salesforce you must pay attention on header information and also body.
Pay attention also on header info hide from Postman, for example if you pass base64 on your body, or a binary expecially, you can encounter problems.

An example of hidden info in header in Postman, these info must be filled also in Salesforce where you don't have an automism to enrich your request.

User-added image

Give us more details to help you if you need
Jatin Gupta 21Jatin Gupta 21
Hi Marzorati,
I have already done the same exercise as suggested by you.i.e. getting all header information from postman to salesforce Apex.I am getting the same result.
I have set the body as blank in the request .I have set the endpoint only.Do I need to do anything for setting the body ?
David Zhu 🔥David Zhu 🔥
You may use httpresposne.getBody() to check the error details
Jatin Gupta 21Jatin Gupta 21
Hi David,
httpresposne.getBody() returns blank whereas httpresponse returns System.HttpResponse[Status=Bad Request, StatusCode=400]
SwethaSwetha (Salesforce Developers) 
HI Jatin,
Can you share your apex code snippet if possible? It looks like parameters are not passed in the proper format. Thanks
Jatin Gupta 21Jatin Gupta 21
Here is the code:
string url = 'https://www.t............html?';
        
        string key = '***********';
        string profile_id = '*********';
        string userid = '*********';
        string start_date = '2020-04-01 17:00';
        string to_date = System.now().format('YYYY-MM-dd HH:mm');
        string endpoint = 'userid='+userid+'&profile_id='+profile_id+'&key='+key+'&from_date='+start_date+'&to_date='+to_date+'&limit=10&page_no=2';
        System.debug('>>>> endpoint >>>>>' + url+endpoint);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader('Content-Type', 'application/json');
        request.setEndpoint(url+endpoint);
        request.setMethod('POST');
        request.setBody('[{}]'); 
        system.debug('====URL===='+request.getEndpoint());
        system.debug('====Header===='+request.getHeader(key));
        HttpResponse response = http.send(request);
        
David Zhu 🔥David Zhu 🔥
It seems the server will parse the data from parameters appended to the endpoint. HTTP request body does not contain any information.
Have you tried removing this line? request.setBody('[{}]');  
Jatin Gupta 21Jatin Gupta 21
Yes I have David
David Zhu 🔥David Zhu 🔥
I notice there are space in these two parameters
  string start_date = '2020-04-01 17:00';    // space between 01 and 17.
string to_date = System.now().format('YYYY-MM-dd HH:mm');   //space between dd and HH

You will haven to encode the parameters before appending the endpoint or try using %20 to replace the space. 
This was selected as the best answer
SwethaSwetha (Salesforce Developers) 
HI Jatin,
Can you see what the debug logs print in salesforce for endpoint and compare it with that of postman and check if there is any difference
Thanks
Jatin Gupta 21Jatin Gupta 21
Thanks David,
Now I have identified the issue.Any blank space should be replaced with %20.It worked for me.
Jatin Gupta 21Jatin Gupta 21
I want to execute a link from my Apex code.Can anybody suggest any way out ?
Marzorati ClaudioMarzorati Claudio
Hi Jatin,

a link to do what?
Please explain better your need

Claudio