How do I handle cookies w/ REST APIs?

Greetings,

I’m new to KNIME, and I’m having trouble with REST and Cookies.

I’m trying to replicate the following bit of code in KNIME:

//codestarts
varunirest=require('unirest');
varcookie_jar=unirest.jar();
varhostname='samplehost.corp.airwave.com';
unirest.post('https://'+hostname+'/LOGIN')
.jar(cookie_jar)
.strictSSL(false)
.headers({
'Content-Type':'application/x-www-form-urlencoded'})
.send({
destination:/index.html'
credential_0:'adminusername',//Usernameforadminuser
credential_1:'adminpassword'//Passwordforadminuser
})
.end(function(resp){
vartoken=resp.headers?resp.headers['x-biscotti']:null;

if(token)
{unirest.get('https://'+hostname+'/amp_stats.xml')
//This is an example of calling amp_stats api.
.jar(cookie_jar)
.strictSSL(false)
.headers({
'Content-Type':'application/xml',
'X-BISCOTTI':token
})
.end(function(res)
{console.log(res.body);
});
} else {

Unfortunately, the server in question is behind a firewall, so I can’t give anyone access for testing.

I can log in to the server via KNIME and retrieve the X-BISCOTTI key. However, I can’t get the cookies/cookie jar.

Below is a Python version, and the requests module using the session function takes care of the cookies in the background:

amp_url = 'https://' + AMP
amp_payload = 'credential_0=' + username + '&credential_1=' + password + '&destination=/&login=Log In'
amp_header = {'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache'}

try:
    with requests.Session() as s:
        s.verify = PROJ_PATH + '/.chain.pem'
        amp_login = s.post(amp_url + '/LOGIN', headers=amp_header, data=amp_payload, timeout=30)

curl works using the -c flag and a cookie jar file.

I understand I haven’t provided much information here, but I’m stumped.

How can I get the cookie jar in KNIME and use the cookies to authenticate the following GET?

Thanks,
Jeff

Hi Jeff,

without further explanation in the code you provided it is rather challenging to comprehend it and its purpose. But given the context of REST + Cookies I assume it’s about authentication.

Have you tried using the either of the two nodes?

Best
Mike

1 Like

Thanks Mike.

Yes, I’ve tried those nodes. I don’t think the cookies are there.

I’ll look again, specifically at Palladian.

Does anyone have any documentation regarding the REST nodes, specifically with respect to Cookie handling?

Thanks,
Jeff

All,

I got it! It was significant trial-and-error and remembering (actually looking up) some client/server fundamentals.

The HTTP Retriever node from Palladian provided the “cookie jar” I was looking for.

Thanks,
Jeff

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.