0

I am trying to send the following curl command in java: curl -v -XPOST -u "7a33f0:" http://geras.1248.io/series/santander/bus3025 --header "Content-Type: application/json" -d@bus3025.json

public boolean writeSeries(String urlstr, String data){
    try {
        // POST
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        System.out.println("POSTING");
        URL url = new URL(urlstr);
        String user = "7a33f0:";
        String encoded = new String(Base64.encodeBase64(user.getBytes()));

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("Authorization", String.format("Basic %s", encoded));
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "*/*");
        connection.setRequestProperty("Content-Length",  Integer.toString(data.getBytes().length));
        connection.setRequestMethod("POST");
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

        writer.write(data);
        writer.flush();
        writer.close();

        int responseCode = connection.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + data);
        System.out.println("Response Code : " + responseCode);

        System.out.println(connection.getContentType());
        InputStream content = (InputStream) connection.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                content));
        String line;
        while ((line = in.readLine()) != null) {
            pw.println(line);
        }
        } catch (MalformedURLException e) {
        // ...
        } catch (IOException e) {
        // ...
        }
    return true;
}

However, I get a 401 response code back for this (Unauthorized).

Sending 'POST' request to URL : http://geras.1248.io/series/santander/bus3024 Post parameters : bus3024.json Response Code : 401 How do I set the -u parameter? Thanks for any help.

schanda
  • 1
  • 3
  • See [here](http://stackoverflow.com/questions/7019997/preemptive-basic-auth-with-httpurlconnection) maybe it helps. – Jens Aug 07 '14 at 14:07
  • Thanks Jens. I added the relevant lines to the code (see edit) but now my function seems to hang after the first print statement. Could it be something to do with the json file to be sent with the POST (I have just specified the file name (the file is in the same folder as the code)) – schanda Aug 07 '14 at 14:54
  • Which is the first print statement? This: ` System.out.println("\nSending 'POST' request to URL : " + url);`? – Jens Aug 07 '14 at 16:20
  • thanks again Jens. I solved the problem by trying the curl command on a terminal with the verbose (-v) option and noting all the headers of the POST. The code needed the content-type, accept & content-length request properties to be set to work. I have updated the code now and it works! – schanda Aug 09 '14 at 11:56

0 Answers0