I have some code that I have been playing around with learning Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class api_example
{
public static void main(String[] args) {
try {
String output;
String input = "{\"status\":\"paused\"}";
URL url = new URL("http://10.0.0.103:4242");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("PUT", "job/4/go/90f76d1455274594ab95a1907c0b0026?api_token=7f94d8f26135723d9fe3ce54");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0)");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, compress");
OutputStream os = connection.getOutputStream();
os.write(input.getBytes());
os.flush();
if (connection.getResponseCode() != 200) {
throw new RuntimeException("failed : Error code : " + connection.getResponseCode());
}
BufferedReader reader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
System.out.println("ouput from Server ... \n");
while ((output = reader.readLine()) != null) {
System.out.println(output);
}
reader.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.lang.RuntimeException: failed : Error code : 404
at api_example.main(api_example.java:37)
which is an attempt to pause a process. Here is a capture of the packet:
POST / HTTP/1.1
PUT: job/4/go/90f76d1455274594ab95a1907c0b0026?api_token=7f94d8f26135723d9fe3ce54
Content-Type: application/json
Accept: */*
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0)
Accept-Encoding: gzip, deflate, compress
Host: 10.0.0.103:4242
Connection: keep-alive
Content-Length: 19
{"status":"paused"}HTTP/1.1 404 Not Found
Content-Type: text/html
Transfer-Encoding: chunked
Date: Wed, 15 Apr 2015 19:17:49 GMT
Server: localhost
9
not found
0
HTTP/1.1 400 Bad Request
Content-Length: 23
Content-Type: text/plain
Illegal end of headers.
Now if I do the same thing using Python:
ipython
import os,requests
data = "{\"status\":\"paused\"}"
heads = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0', 'Content-type': 'application/json'}
paused = requests.put('http://10.0.0.103:4242/job/4/go/90f76d1455274594ab95a1907c0b0026?api_token=7f94d8f26135723d9fe3ce54', data=data, headers=heads)
paused
<Response [200]>
which works as expected. Here is a success packet capture of it:
PUT /job/4/go/90f76d1455274594ab95a1907c0b0026?api_token=7f94d8f26135723d9fe3ce54 HTTP/1.1
Host: 10.0.0.103:4242
Content-Length: 21
Content-type: application/json
Accept-Encoding: gzip, deflate, compress
Accept: */*
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0
{"status":"want now"}HTTP/1.1 200 OK
Content-Length: 32
Content-Type: application/json
Date: Wed, 15 Apr 2015 19:02:42 GMT
Server: localhost
"Request completed successfully"
What am I missing on the java side?