Search code examples
salesforceapex-code

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.


Solution

  • 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.