iRule - command is not valid in the current event context - cookie management

I am attempting to write an iRule that checks a cookie on HTTP_REQUEST and then writes a cookie on HTTP_RESPONSE in order to control the display of a banner message:

when CLIENT_ACCEPTED {
    set ckname NotificationDone
    set ckvalue 1
    set ckdomain .[HTTP::host]
    }

when RULE_INIT {    
                set static::notification_page {
        ... HTML Notification Page - code omitted ...                }  
}

when HTTP_REQUEST  {
                if { (not [HTTP::cookie exists NotificationDone]) } {
                                HTTP::respond 200 content [subst $static::notification_page] Mime-Type "text/html" 
                }
}

when HTTP_RESPONSE {
        if { (not [HTTP::cookie exists NotificationDone]) } {
            HTTP::cookie insert name $ckname value $ckvalue path / domain $ckdomain
            HTTP::cookie expires $ckname 120
        }
    }

When I run this code I get: “[command is not valid in the current event context(CLIENT_ACCEPTED)][HTTP::host]” I also get an similar error in the HTTP_RESPONSE when I attempt to set the cookie.

I’ve seen a number of examples that use this same pattern so I’m not sure what I am missing. Any help would be greatly appreciated.

Kevin

The command is on wrong layer place.

I have not tested, but I think it will work:

when RULE_INIT {    
    set static::notification_page {
        ... HTML Notification Page - code omitted ...                
    }
}

when HTTP_REQUEST  {
    set ckname NotificationDone
    set ckvalue 1
    set ckdomain ".[HTTP::host]"

    if { (not [HTTP::cookie exists $ckname]) } {
        HTTP::respond 200 content [subst $static::notification_page] Mime-Type "text/html" 
    }
}

when HTTP_RESPONSE {
    if { (not [HTTP::cookie exists $ckname]) } {
        HTTP::cookie insert name $ckname value $ckvalue path / domain $ckdomain
        HTTP::cookie expires $ckname 120
    }
}

Regards.