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
pavan kumar 177pavan kumar 177 

Status Returned 200 but response body is blank

public class AuthCallout {
    public void basicAccessTokenGeneration() {
     HttpRequest req = new HttpRequest();
     req.setEndpoint('https://');
     req.setMethod('POST');

     // Specify the required username and password to access the endpoint
     // As well as the header and header information

     String username = '';
     String password = '';
     String vendor   = '';
     String businessunit ='';
     String application ='';

     Blob headerValue = Blob.valueOf(application + '@' + vendor + ':' + businessunit);
     String authorizationHeader = 'BASIC ' +
     EncodingUtil.base64Encode(headerValue);
     req.setHeader('Authorization', authorizationHeader);


    JSONGenerator gen = JSON.createGenerator(true);    

    gen.writeStartObject();      
    gen.writeStringField('grant_type ', 'password');
    gen.writeStringField('username', username);
    gen.writeStringField('password',password);
    gen.writeStringField('scope','');
    gen.writeEndObject();
    String jsonS = gen.getAsString();
    system.debug('DAta in '+str);
    system.debug('Data in gen'+jsonS);
    req.setBody(jsonS);
     // Create a new http object to send the request object
     // A response object is generated as a result of the request  

     Http http = new Http();
     HTTPResponse res = http.send(req);
     System.debug(res.getBody());
        System.debug(res.getStatusCode());
        System.debug(res.getBodyDocument());
   }
}

'm new to integration. I am going to generate an access token for the third-party application.
I tried to build following javascript request in sfdc.

But the response body is blank. I need to retrieve access token & other things to call specific endpoint in another class.
Please let me know where i did wrong

I am searlize JSON string also while sending the body

They gave same code request sample in java format like below.

Sample request
var result = {};
var application = "";
var vendor = "";
var businessunit = "";
var user = "";
var pass = "";
var authCode = window.btoa(application + "@" + vendor + ":" + businessunit);
$.ajax({
    "url": 'https://',
    "type": 'post',
    "contentType": 'application/json',
    "dataType": 'json',
    "headers": {
        'Authorization': 'basic ' + authCode
    },
    "data": JSON.stringify({
    "grant_type": 'password',
    "username": user,
    "password" : pass,
    "scope": 'AdminApi AgentApi AuthenticationApi PatronApi RealTimeApi'
    }),
    "success": function (resp) {
        result.access_token = resp.access_token;
        result.token_type = resp.token_type;
        result.resource_server_base_uri = resp.resource_server_base_uri;
        result.expires_in = resp.expires_in;
        result.refresh_token = resp.refresh_token;
        result.scope = resp.scope;
        result.refresh_token_server_uri = resp.refresh_token_server_uri;
    },
    "error": function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Failed to retrieve token.\n" + XMLHttpRequest.status + ' ' 
            + XMLHttpRequest.statusText);
    }
});

My Apex

 
bretondevbretondev
Here :
req.setEndpoint('https://');

You are sending your request to the void. How can you expect an answer?
There is no service treating your request.

Your endpoint should look like something like that :
req.setEndpoint('https://somedomain/someoptionalsubdomain/etcetra');

There's got to be a specific endpoint for you request
pavan kumar 177pavan kumar 177
For privacy issues. I put it blank in my question. but i am passing an endpoint https with all other details.

Even if i gave wrong credentials still i got same status code.

Please let me know where i missed it
bretondevbretondev
OK.
Well maybe you are doing it right but there is an error at the level of the service provider.- that's very possible  (do you have contact with them?)
Check with them and see how they handle your request.

Otherwise you can try generating your request body in another way by creating a Wrapper object and use JSON.serialize().

That would be somethin like this :
RequestWrapper rw = new RequestWrapper();
		rw.username = myUsername
		rw.password = myPassword;
		
		String JSONrequestbody = JSON.serialize(rw);
		req.setBody(JSONrequestbody);
		
		Http http = new Http();
	    HTTPResponse res = http.send(req);
		

		private class RequestWrapper {
		
			String username;
			String password;
			//etc..
		}


 
NagendraNagendra (Salesforce Developers) 
Hi Pavan,

May I suggest you please check the client password you are using in above API call which might be causing the issue.

If the issue is with password then I would suggest you please give a try by resetting the password which should probably do the trick.

Hope this helps.

Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra
CHAYAN BATABYALCHAYAN BATABYAL
Hi @pavan kumar 177,

I'm facing the same issue when requesting the access token for reporting API of nice InContact through Apex. Can you please let us know what was the issue and how did you resolve it?

Thanks,
Chayan Batabyal