Hi everyone,
I’m experiencing a very annoying problem.
I need to use a rest Service that accepts only application/x-www-form-urlencoded as content-Type
I have made a Python scriptr that works and I’m trying to replicate it with Post service node.
Though I have some troubles to find how to pass the form in the body when there are present reserved characters.
there’s the python script:
I should pass in the body this string “smiles=CC(C)CC1%3DCC%3DC(C%3DC1)C(C)C(%3DO)O”
but somehow it doesnnt’ work. I tried different encodings nothing seems to work.
when i pass a string withoun unreserved characters (like “smiles=CC”) everything seems to be ok.
What I’m wrong?
Thanks, I tried but it didn’t work, the solution i found was to use a java snippet with a script i found here in the forum
`/ 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;
// imports for input and output fields
import java.io.InputStream;
//import java.io.IOUtils;
// system variables
public class JSnippet extends AbstractJSnippet {
// Fields for input columns
/** Input column: “Entity” */
public InputStream c_body;
// Fields for output columns
/** Output column: “Out” */
public String out_abc;
// Your custom variables:
// expression start
public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
try {
URL url = new URL(“http://www.swissadme.ch/index.php”);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
List<Byte> cBodArry = new ArrayList<Byte>();
Reader r = new BufferedReader(new InputStreamReader(c_body));
for(int c; (c = r.read()) >= 0;)
cBodArry.add((byte)c);
byte[] postDataBytes = new byte[cBodArry.size()];
int i=0;
for(byte b: cBodArry)
postDataBytes[i++] = b;
InputStreamReader reader = new InputStreamReader(c_body);
conn.setRequestMethod("POST");
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
out_abc = in.lines().collect(Collectors.joining(""));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}