Body charset in a Post Service

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:

import requests

url = “SwissADME

payload = {‘smiles’:“CC(C)CC1=CC=C(C=C1)C(C)C(=O)O”}
headers = {
‘origin’: “http://www.swissadme.ch”,
‘upgrade-insecure-requests’: “1”,
‘user-agent’: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36”,
‘accept’: “text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8”,
‘referer’: “SwissADME”,
‘accept-encoding’: “gzip, deflate”,
‘accept-language’: “it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7”,
‘cookie’: “_ga=GA1.2.501643331.1529332896; _gid=GA1.2.1536978171.1530717977”,
‘content-type’: “application/x-www-form-urlencoded”,
‘cache-control’: “no-cache”,
‘postman-token’: “9293e3ff-9ef9-8ac8-e179-9707df3d5fac”
}

response = requests.request(“POST”, url, data=payload, headers=headers)

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?

Ok, watching the output of my topic I found part of the solution: ( C ) is interpreted like copyright symbol.
How I can overcome this trouble?

Hi @cosimotoma,

Please see here for an example how to create a POST request with application/x-www-form-urlencoded body (in the bottom “Parameter input” metanode): https://www.knime.com/nodeguide/data-access/rest-web-services/sugarcrm-meets-salesforce

This should also take care of your encoding problem via the FormEncodedHttpEntityCreator from the Palladian community extensions.

Hope that helps!

Cheers,
Roland

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;

// Your custom imports:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

//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();
	}

// expression end
}
}
`

1 Like

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