Anutosh Datta
Anutosh Datta

Reputation: 419

Setting cookie from webservice call out in salesforce

Can somebody please explain me the below?

stub.inputHttpheaders_x.put('Cookie', 'name = value');

What is 'name = value' in this case?

I am getting the cookie as below:

stub.outputHttpheaders_x.get('Set-cookie');

How do i use this cookie in the first statement?

Thanks in advance.

Upvotes: 1

Views: 2582

Answers (2)

TheShadow
TheShadow

Reputation: 631

To be precise:

You have to "enable" outputHttpHeaders_x by initialize the map fist Afterwards you can access the Set-Cookie.

Can be read here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex.htm

The value of outputHttpHeaders_x is null by default. You must set outputHttpHeaders_x before you have access to the content of headers in the response.

docSample.DocSamplePort stub = new docSample.DocSamplePort();
stub.outputHttpHeaders_x = new Map<String, String>();
String input = 'This is the input string';
String output = stub.EchoString(input);

//Getting cookie header
String cookie = stub.outputHttpHeaders_x.get('Set-Cookie');

//Getting custom header
String myHeader = stub.outputHttpHeaders_x.get('My-Header');

Upvotes: 0

Anup
Anup

Reputation: 1011

When you get hold of the stub, you can set the HTTP Header fields using the inputHttpheaders_x.put method.

This Wikipedia link has a good description of what fields you can set on a HTTP Header. One of these fields to set is "Cookie". It can be set to a "key=value" value for e.g. "site=google".

The code block

stub.inputHttpheaders_x.put('Cookie', 'name = value');

sets the value 'name = value' to the Cookie header field.

Similarly, you can access cookie value set in the HTTP Headers on a response object using:

String cookie = stub.outputHttpHeaders_x.get('Set-Cookie')

Hope this makes sense!

Anup P.S: If you are trying this with a proper integration setup. Try printing the values out to understand the format of the output.

Upvotes: 1

Related Questions