2

Not understanding why I am getting this error if I check this URL in Soap UI it is working perfectly. Getting this error after setting body 'Input is not valid JSON at character 1 of '

public static void request(String S,String cd) {
        string customer_code=cd; 
        String SalesOrganization=s;   
        String username ='****';
        String password ='*****';
        string payload='[{"SalesOrganization"  : "solidealusa","customer_code"  : "CS000023"}]';
        Blob headerValue = Blob.valueOf(username+':'+password);
        String authorizationHeader ='BASIC'+EncodingUtil.base64Encode(headerValue);    

        HttpRequest req = new HttpRequest();    
        req.setEndpoint('https://da1.camso.co:50001/RESTAdapter/sold_to/batch');
        req.setMethod('POST');
        req.setHeader('Authorization', authorizationHeader);
        req.setBody(payload);
        req.setCompressed(false); // otherwise we hit a limit of 32000    
      try 
      {
         HttpResponse res = new Http().send(req);     
         system.debug('Response' +res.getBody());          
        } catch(System.CalloutException e) {
        System.debug('Callout error: '+ e);        
        }
     }

1 Answer 1

4

Some keywords in web services (SOAP or REST) are case sensitive. For example, Basic, Bearer, Hybrid all should be camel cased (First letter capital and remaining small). Also there should be space between the type of authorization (Basic, Bearer, Hybrid) and the encoded authorization value. So change this

String authorizationHeader ='BASIC'+EncodingUtil.base64Encode(headerValue);

to

String authorizationHeader ='Basic '+EncodingUtil.base64Encode(headerValue);

Hope it helps.

3
  • Awesome it worked ! But now I am getting this error "Input is not valid JSON at character 17 of SalesOrganization=solidealusa&customer_code=CS000023"" when i am testing from anonymous block Commented Apr 4, 2016 at 14:37
  • I guess you need to formulate JSON and then set it to body of the request. The way you are setting the body now it like passing encoded parameter values which your receiver rejects. I would suggest you to post it as a separate question to attract more quality answers. Commented Apr 4, 2016 at 14:42
  • Thanks I have updated the question ..followed same process but still I could get the error. Commented Apr 4, 2016 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .