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
smita bhargavasmita bhargava 

status code=400 issue

I am learning the basics of rest api integration.
I am using two sfdc developer orgnzs. flipkart(is the source) and hdfc(is the destination).

I am calling a webservice defined in destination from source to check if an account name already exists.

I am using OAUTH2.0 authentication and I have access token generated.

code in destination side.
code in destination side
===================
@RestResource(UrlMapping='/RestMapping1/*')
global class RestObject
{
@HTTPGet
   webservice static void SearchByAccountName()
   {
     String account_name=RestContext.request.params.get('accName');
     
     system.debug('account Name found in destination = '+account_Name);
     
   }
}

code in source side
==============
Public PageReference SearchByAccountName()
       {
         if (accesstoken != NULL)
         {
       
           HTTP h = new HTTP();
     
           HTTPRequest req = new HTTPrequest();
     
           req.setMethod('GET');
     
           req.setHeader('Authorization','Bearer '+accesstoken);
       
           req.setHeader('Content-Type','application/json; charset=UTF-8');
       
           req.setHeader('Accept','application/json');
       
           acc=[select Name from Account where Name LIKE '%Sunshine%' LIMIT 1];
           
           req.setEndpoint('https://ap2.salesforce.com/services/apexrest/RestMapping1?accName='+acc.Name);
           
           HTTPResponse resp;
           try
           {
             resp=h.send(req);
             string json=resp.getBody();
             system.debug('json = '+json);
            
           }
       
           catch(system.CallOutException ex)
           {
           
           }

when I check debug logs I am getting statuscode=400 (bad request).
Can any one tell me where is the issue and how to resolve it?

smita
Best Answer chosen by smita bhargava
Om PrakashOm Prakash
Hi Smita,
You need to encode the parameter value in url (Line number 37)   EncodingUtil.urlEncode(acc.Name, 'UTF-8')
req.setEndpoint('https://ap5.salesforce.com/services/apexrest/oauthcon/RestMapping1?accName='+EncodingUtil.urlEncode(acc.Name, 'UTF-8'));

 

All Answers

Manj_SFDCManj_SFDC
Hi Smita, 
did you check what your query is returning at line 35?
smita bhargavasmita bhargava
Hi mani
I checked the query in dev console. It is returning a sngle record. Here is the result from debug logs

Endpoint=https://ap2.salesforce.com/services/apexrest/RestMapping1?accName=sunshine Hospitals, Method=GET] 
System.HttpResponse[Status=Unknown Version, StatusCode=400] 08:25:40.0 (797210626)|HEAP_ALLOCATE|[222]|Bytes:77

 
Om PrakashOm Prakash
Hi Smita,
You need to encode the parameter value in url (Line number 37)   EncodingUtil.urlEncode(acc.Name, 'UTF-8')
req.setEndpoint('https://ap5.salesforce.com/services/apexrest/oauthcon/RestMapping1?accName='+EncodingUtil.urlEncode(acc.Name, 'UTF-8'));

 
This was selected as the best answer
Manj_SFDCManj_SFDC
 Hello Smita  please verify the remote site settings it should have the end point URL added
Manj_SFDCManj_SFDC
And also try to use SoapUI to post the request without apex , you will get detailed results which may help you to figure out why was it a bad request
smita bhargavasmita bhargava
Hello
Thanks that worked