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
Hardik ChavdaHardik Chavda 

Getting this error : System.HttpResponse[Status=Bad Request, StatusCode=400]

Hi,

I am actually new to REST api and tried one simple example by connecting 2 salesforce org. Following is the snippet of my code.

Source Org Class.

public class RestServiceCalloutController{
    
    public void CalloutRest(){
        try{
            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://ap2.salesforce.com/services/apexrest/TestAccount');
            req.setMethod('POST');
            String authorizationHeader = 'Bearer '+SessionId; //put the sessionId manually from the destination org.
            req.setHeader('Authorization', authorizationHeader);
            //req.setHeader('Content-Type', 'application/json');
            system.debug('*******REQ'+req);
            
            Http http = new Http();
            HTTPResponse res = http.send(req);
            system.debug('*******Res'+res);
        }
        catch(exception e){
            system.debug('--------EXCEPTION-----'+e);
        }
    }
 
}
Destination Org Class.
 
@RestResource(urlMapping='/TestAccount/*')
global class TestRestApi{
    
        @HttpPost
        global static void TestRest(){
                system.debug('Caaling');
        }
}

When I call the code from the source org. It sends the request but in response it gives me back this error : System.HttpResponse[Status=Bad Request, StatusCode=400],

I even tried using SOAP UI, It shows me the following error. 

User-added image

I also went to critical updates and activate the TLS 1.1, Can anyone please help me with this?

Thanks in advance.
Harish RamachandruniHarish Ramachandruni
Hi,


Create site in your sales force account of which is havingrest class .


replace 


            req.setEndpoint('yoursiteurl/services/apexrest/TestAccount');
give a acees to class for site profile ,

it will work .


Regads,
Harish.R.