elodina / dropwizard-kafka-http

Apache Kafka HTTP Endpoint for producing and consuming messages from topics
http://www.elodina.net
Apache License 2.0
154 stars 45 forks source link

Unable to post message on HTTP REST URL #15

Open aniruddha-github opened 9 years ago

aniruddha-github commented 9 years ago

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

topic=topic1 message={"item":"pen", "Qty":3} getting http response as HTTP/1.1 400 Bad Request Date: Sun, 14 Jun 2015 12:09:02 GMT Content-Type: application/json Transfer-Encoding: chunked Any clue what is missing ? Also how to assing port other than 8080 ? Thanks in advance Apparently no erros in Kafka server logs or zookeeper logs on console
dmitrypekar commented 9 years ago

Hi, Please use correctly URL-encoded POST body. Content-Type should be "application/x-www-form-urlencoded"

aniruddha-github commented 9 years ago

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

topic%3Dtopic1%0Amessage%3D%7B%22item%22%3A%22pen%22%2C+%22Qty%22%3A3%7D

HTTP/1.1 400 Bad Request Date: Sun, 14 Jun 2015 15:01:50 GMT Content-Type: application/json Transfer-Encoding: chunked

["Undefined topic","Undefined message"]

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

    }

}

}

dmitrypekar commented 9 years ago

Please use normal form query string (without leading question mark) format as POST body.