+ Start a Discussion
Abdul PatelAbdul Patel 

Setting sessionId in Http Request Header

I am doing a POST call to external rest resource. The authorization is 2 step process and I need to pass the JSESSIONID in the second call which is received in first call.

With the POSTMAN, it works fine. Not sure why Apex is giving me 302 as the response status code. Can someone please help what is wrong in my code?

Body-
User-added image
Header - 
User-added image

Below is my code for Http Request -


HttpRequest req1 = new HttpRequest();
String body = 'username=' + username + '&password=' + password + '&_csrf=' + csrf_token;
req1.setEndpoint('https://onlinehearing-backend.cloudapi-dev.sivantos.com/admin/login');
req1.setMethod('POST');
req1.setBody(body);
req1.setHeader('Cookie', sessionIdString); // sessionIdString is sessionId in format -  JSESSIONID=69C4096F47BB59594D5D6C45A20A166C
//req1.setHeader('Authorization',  'Bearer '+sessionId);

Http http1 = new Http();
HTTPResponse resp = http1.send(req1);
System.debug('resp>>' + resp);
Ramesh DepaiahRamesh Depaiah
@abdual
Sometimes you need to send body along with url so try something like 
HttpRequest req1 = new HttpRequest();
String body = 'username=' + username + '&password=' + password + '&_csrf=' + csrf_token;
req1.setEndpoint('https://onlinehearing-backend.cloudapi-dev.sivantos.com/admin/login'+body);
req1.setMethod('POST');
//req1.setBody(body);
req1.setHeader('Cookie', sessionIdString); // sessionIdString is sessionId in format -  JSESSIONID=69C4096F47BB59594D5D6C45A20A166C
//req1.setHeader('Authorization',  'Bearer '+sessionId);

Http http1 = new Http();
HTTPResponse resp = http1.send(req1);
System.debug('resp>>' + resp);

Thanks
Ramesh
 
Ramesh DepaiahRamesh Depaiah
Sorry try this i forgot ? 
HttpRequest req1 = new HttpRequest();
String body = 'username=' + username + '&password=' + password + '&_csrf=' + csrf_token;
req1.setEndpoint('https://onlinehearing-backend.cloudapi-dev.sivantos.com/admin/login?'+body);
req1.setMethod('POST');
//req1.setBody(body);
req1.setHeader('Cookie', sessionIdString); // sessionIdString is sessionId in format -  JSESSIONID=69C4096F47BB59594D5D6C45A20A166C
//req1.setHeader('Authorization',  'Bearer '+sessionId);

Http http1 = new Http();
HTTPResponse resp = http1.send(req1);
System.debug('resp>>' + resp);

 
Abdul PatelAbdul Patel
@Ramesh : Thank you for your response. I gave a try with your code and I am receiving 403 status code. Are we missing something or there is something to be done by the provider :(
Ramesh DepaiahRamesh Depaiah
@abdual check the provider API document and see what response codes they are sending