+ Start a Discussion
ManojKumar MuthuManojKumar Muthu 

System.HttpResponse[Status=Unauthorized, StatusCode=401

I got stuck here, can someone help to slove this error,
below my code,
trigger Level2Update on Account_Case_Escalation__c (after update) {

for(Account_Case_Escalation__c ACE: trigger.new)
util.callwebservice(ace.Account__c, ace.Level_2_Name__c, ace.Level_2_Email__c, ace.L2_Title__c);
system.debug('Account Case account  '+ ace.Account__c);
system.debug('Account Case email  '+ ace.Level_2_Email__c);
system.debug('Account Case name  '+ ace.Level_2_Name__c);
system.debug('Account Case Title  '+ ace.L2_Title__c);



Apex Class:

public with sharing class util
public static  void callwebservice(Id Accid, String name, String email, String title)

HTTP auth = new HTTP()    ;
//List<contact> acc = [Select Id, Name from Account Where Id IN :recordIds];
//String Body = "Accountid":cid + "id":aid;

HTTPRequest r = new HTTPRequest();
String username = 'appviewxsupport2@yopmail.com';
String password = 'Appviewx@123';

Blob headerValue = Blob.valueOf(username+ ':' + password );
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
r.setHeader('Authorization', authorizationHeader);
HTTPResponse authresp=new HttpResponse();
authresp = auth.send(r);
system.debug('Statuscode Expected'+ authresp.getStatusCode());
System.debug('Body expected' + authresp.getBody());

When I run the above code below the error i facing,
Error: System.HttpResponse[Status=Unauthorized, StatusCode=401

Raj VakatiRaj Vakati
The endpoint is returning 401 Unauthorized. Possible reasons:

1.Bad request URL. Copy-pasting URL from your code to a browser may validate.
2.Wrong protocol. If the endpoint requires HTTPS and your URL uses HTTP.
3.Bad IP. You said that the provider has whitelisted the SFDC IPs, but perhaps they made an error. If you are able to test from a browser from a non-4.whitelisted IP (e.g., from home), you can determine if you get the same error.
5.Some other error at the endpoint. If everything else fails, you may need to ask the provider to check their logs.

Can you send the Same API call from the using POSTMAN and see 
ManojKumar MuthuManojKumar Muthu
Hi Raj,
Thanks the response, I tried all the above, nothing came out, I postman its working fine.
what I see here is I have to pass the access token, that were i got stuck
Raj VakatiRaj Vakati
You need to pass access token like below  .. please check with the  your rest servcei what type of params we need to pass 
Http h = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
HttpResponse response = h.send(request);
System.debug('response '+response.getBody());

Santosh Reddy MaddhuriSantosh Reddy Maddhuri

Hi Manojkumar,

This issue is stemming from the fact that you might be using invalid credentials. When i directly used the url and user+pass into browser url below is the response xml.

User-added image

hope this helps!



ManojKumar MuthuManojKumar Muthu
Hi, Santosh Reddy Maddhuri,

Yes, its because we are not passing the access token