Camel HTTP Endpoint: How to set URL-String to POST Parameter
10,404
As isim mentioned above, following works for me. The idea is to parse a given url fist and to encode it again afterwards. This avoids double encoding.
import java.io.UnsupportedEncodingException;
import java.net.*;
public static String getEncodedURL(String urlString) {
final String encodedURL;
try {
String decodedURL = URLDecoder.decode(urlString, "UTF-8");
URL url = new URL(decodedURL);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
final URL urlFromDecoding = uri.toURL();
encodedURL = URLEncoder.encode(urlFromDecoding.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
...
} catch (MalformedURLException e) {
...
} catch (URISyntaxException e) {
...
}
return encodedURL;
}
Author by
Max
Updated on June 05, 2022Comments
-
Max almost 2 years
Prerequisites
- Apache Tomcat 7
- Spring 3.2.11.RELEASE
- Apache Camel 2.14.1
- Camel HTTP Endpoint (
<artifactId>camel-http</artifactId>
)
Problem
At the moment I use following code to set POST-Parameters to the message body. The camel HTTP-Component reads the parameters and sends it.
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST.name())) .setHeader(Exchange.CONTENT_TYPE, constant("application/x-www-form-urlencoded; charset: UTF-8")) .setHeader(Exchange.CONTENT_ENCODING, constant("UTF-8")) .setBody("parameter1=a¶meter2=b")
The problem ist that some parameters are URLs itself. So something like this should be send as POST-Request:
postparameter1=a&postparameter2=http://www.`...`.com?urlparam1=value1&urlparam2=value2&postparameter3=b
My question is how to send "http://www.
...
.com?urlparam1=value1&urlparam2=value2" as value of postparameter2.Thanks in advance.
Regards,
Max