REST API Call with "Authorization: Token"

Hello together, 

I am struggeling to get the following cURL call to work as a Java Snippet node:

curl -X GET -H "x-sessiontoken: $2a$04$7UhCb2pm0aH.HUMm3Dn7lu" -H "Cache-Control: no-cache" -H "Postman-Token: bb01f25c-f1a9-79f8-b77c-0b7532b70629" [REST URL]

I found the following code in the forum (https://tech.knime.org/forum/krest-nodes/api-call-with-authorization-token?destination=node%2F48743) but I don't know what needs to be changed to get it to work and output the JSON answer of the REST service.

// system imports
import org.knime.base.node.jsnippet.expression.AbstractJSnippet;
import org.knime.base.node.jsnippet.expression.Abort;
import org.knime.base.node.jsnippet.expression.Cell;
import org.knime.base.node.jsnippet.expression.ColumnException;
import org.knime.base.node.jsnippet.expression.TypeException;
import static org.knime.base.node.jsnippet.expression.Type.*;
import java.util.Date;
import java.util.Calendar;
import org.w3c.dom.Document;
 
// Your custom imports:
import javax.xml.bind.DatatypeConverter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.io.PrintStream;
import org.knime.core.node.NodeLogger;
// system variables
public class JSnippet extends AbstractJSnippet {
  // Fields for input columns
/** Input column: "username" */
  public String c_username;
/** Input column: "password" */
  public String c_password;
/** Input column: "auth_url" */
  public String c_auth_url;
 
  // Fields for output flow variables
/** Output flow variable: "toLWSSO" */
  public String out_toLWSSO;
 
// Your custom variables:
 
// expression start
  public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
 
         
        String enc_auth = DatatypeConverter.printBase64Binary(String.format("%s:%s", c_username, c_password).getBytes());
try{
        // Open connection
        HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(c_auth_url).openConnection();
 
         
        // Clean Accept
        httpUrlConnection.setRequestProperty("Accept", "text/plain");
 
        Map<String,String> headers = new HashMap<>();
        headers.put("Authorization", "Basic " + enc_auth);
 
        // Add HTTP headers
        if (!headers.isEmpty()) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpUrlConnection.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
 
         
        // Set HTTP method
        httpUrlConnection.setRequestMethod("GET");
 
         
        // Set response statusCode
       httpUrlConnection.getResponseCode();
 
 
         Map<String, List<String>> responseHeaders = httpUrlConnection.getHeaderFields();
          
         List<String> allrespHeaders = responseHeaders.get("Set-Cookie");
          
         NodeLogger LOGGER = NodeLogger.getLogger(AbstractJSnippet.class);
 
         for(int i=0; i<allrespHeaders.size();i++){
            String str = allrespHeaders.get(i);
            if(str.contains("COOKIE_KEY")){
                out_token = str;    
                LOGGER.warn("Found: " + str);
            }
         }
}
catch (Exception ex)
{}   
 
// expression end
    }
}

Is anybody able to help me out with the necessary code or where I might find an answer on that?

Any help is appreciated!

Many thanks an best regards, Andy 

Why don't you use the new REST nodes? This will save you from writing a single line of Java code and setting custom headers is really easy.

Hi Thor, 

thanks a lot for the hint. Works perfectly with the REST nodes from the lab. 

Best, Andy

 

Can you share how you fix this by REST nodes? I am also struggling in the authorization token.