<h1>Reading and Writing cookies</h1>

<p>You can use the <code>cookies</code> parameter, which maps cookie names to fields that are present in messages.</p>

<p>For example, <code>.cookies.auth_token = "token"</code> binds the cookie called <code>auth_token</code> to the field <code>token</code> in messages (both in reading and writing).</p>

<p>Here's a complete example where the <code>login</code> operation sets the cookie <code>auth_token</code> in the browser.</p>

<pre><code>execution { concurrent }

inputPort Server {
Location: "socket://localhost:8080"
Protocol: http
  // Binds the cookie "auth_token" to the message field "token"
  { .cookies.auth_token = "token" }
RequestResponse: login
}

main
{
  login( request )( response ) {
    if ( request.pwd == "secret" )
      response &lt;&lt; "OK" { .token = new }
    else
      response &lt;&lt; "Invalid pwd" { .token = "" }
  }
}
</code></pre>

<p>You can try it by browsing <a href="http://localhost:8080/login?pwd=secret" rel="nofollow noreferrer">http://localhost:8080/login?pwd=secret</a>.</p>

<hr>

<h1>Bonus: cookies with correlation sets</h1>

<p>Using cookies like this allows for combining them with workflows, to program <a href="https://doi.org/10.1016/j.scico.2016.05.002" rel="nofollow noreferrer">process-aware web applications</a>. Here is a more elaborate example with the following workflow:</p>

<ol>
<li>The user logs in;</li>
<li>If the login is successful, operation <code>say</code> can be called at will, until operation <code>logout</code> is called;</li>
<li>The user logs out.</li>
</ol>

<p>I'm using correlation sets below to track the session.</p>

<pre><code>include "console.iol"

execution { concurrent }

type LoginRequest:void { .token?:string .pwd:string }
type TokenMessage:void { .token:string }
type SayRequest:void { .token:string .msg:string }

interface ServerIface {
RequestResponse:
  login(LoginRequest)(TokenMessage) throws InvalidPwd,
  say(SayRequest)(void),
  logout(TokenMessage)(TokenMessage)
}

inputPort Server {
Location: "socket://localhost:8080"
Protocol: http { .cookies.auth_token = "token" }
Interfaces: ServerIface
}

cset {
  token: SayRequest.token TokenMessage.token
}

main
{
  login( request )( response ) {
    if ( request.pwd == "secret" )
      response.token = csets.token = new
    else
      throw( InvalidPwd )
  };
  provide
    [ say( request )() {
      println@Console( csets.token + " says " + request.msg )()
    } ]
  until
    [ logout()( response ) { response.token = "" } ]
}
</code></pre>

<p>To try it, you can navigate to these links (in order):</p>

<ol>
<li><a href="http://localhost:8080/login?pwd=secret" rel="nofollow noreferrer">http://localhost:8080/login?pwd=secret</a></li>
<li><a href="http://localhost:8080/say?msg=Hello" rel="nofollow noreferrer">http://localhost:8080/say?msg=Hello</a></li>
<li><a href="http://localhost:8080/logout" rel="nofollow noreferrer">http://localhost:8080/logout</a></li>
</ol>

<p>References:</p>

<ul>
<li>Sessions and correlation sets: <a href="https://jolielang.gitbook.io/docs/basics/sessions" rel="nofollow noreferrer">https://jolielang.gitbook.io/docs/basics/sessions</a></li>
<li>provide-until statement: <a href="https://jolielang.gitbook.io/docs/basics/composing_statements#the-provide-until-statement" rel="nofollow noreferrer">https://jolielang.gitbook.io/docs/basics/composing_statements#the-provide-until-statement</a></li>
<li>The paper on provide-until and web workflows in Jolie: <a href="https://doi.org/10.1016/j.scico.2016.05.002" rel="nofollow noreferrer">https://doi.org/10.1016/j.scico.2016.05.002</a> (open version: <a href="https://arxiv.org/abs/1410.3712" rel="nofollow noreferrer">https://arxiv.org/abs/1410.3712</a>)</li>
</ul>
How to set http cookies from an operation of a Jolie service?
<p>I need to set a cookie to the browser from an operation of a Jolie service, but I can't find information on how to do it.</p>

<p>I checked the doc at <a href="https://jolielang.gitbook.io/docs/protocols/http" rel="nofollow noreferrer">https://jolielang.gitbook.io/docs/protocols/http</a> and <a href="https://jolielang.gitbook.io/docs/web-applications/rest-apis-publication-and-integration" rel="nofollow noreferrer">https://jolielang.gitbook.io/docs/web-applications/rest-apis-publication-and-integration</a>, but it seems the use case I presented has not yet been covered.</p>

<pre><code>inputPort MyIP {

    Protocol: http {
    ...
    ??? -&gt; myCookie;
    ...
    }
    Interfaces: LoginInterface
}

main {

    [login(credentials)(res) {
    ...  
    myCookie=???  
    ...  
    }]
}
</code></pre>

<p>I expect to see the cookie in the browser cookie store.</p>