Open aniruddha-github opened 9 years ago
Hi, Please use correctly URL-encoded POST body. Content-Type should be "application/x-www-form-urlencoded"
Thanks. After above changes, here is the HTTP REQ and Res. in TCPMon POST /message HTTP/1.1 Content-Type: application/x-www-form-urlencoded User-Agent: Java/1.7.055 Host: localhost:8080 Accept: text/html, image/gif, image/jpeg, ; q=.2, */_; q=.2 Connection: keep-alive Content-Length: 72
HTTP/1.1 400 Bad Request Date: Sun, 14 Jun 2015 15:01:50 GMT Content-Type: application/json Transfer-Encoding: chunked
here is my java code package kafka.examples;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder;
public class RestProducer {
private static URL url = null;
public static void main(String[] agrs) {
HttpURLConnection conn = null;
try {
url = new URL(KafkaProperties.KafkaRESTUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String topic = "topic=topic1\n";
String msg = "message=" + "{\"item\":\"pen\", \"Qty\":3}";
String key = "key=0\n";
String fullMessage = URLEncoder.encode(topic, "UTF-8") + URLEncoder.encode(msg, "UTF-8");
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(fullMessage);
out.close();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (null != conn)
conn.disconnect();
}
}
}
Please use normal form query string (without leading question mark) format as POST body.
Using Java code to put message on HTTP REST Kafka server. TCP Monitor captured request details as below
POST /message HTTP/1.1 Content-Type: application/json User-Agent: Java/1.7.055 Host: localhost:8080 Accept: text/html, image/gif, image/jpeg, ; q=.2, */_; q=.2 Connection: keep-alive Content-Length: 45