Java HTTP PUT Request/JSON Not Working But Using Python It Does ?


 
Thread Tools Search this Thread
Top Forums Programming Java HTTP PUT Request/JSON Not Working But Using Python It Does ?
# 1  
Old 04-15-2015
Java HTTP PUT Request/JSON Not Working But Using Python It Does ?

I have some code that I have been playing around with learning Java:
Code:
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();
    }
  }
}

Code:
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:
Code:
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:
Code:
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:
Code:
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?
# 2  
Old 04-16-2015
Hi,
Maybe not important, but your User-Agent is different between java and python:
Python:
Code:
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0

Java:
Code:
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0)

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tweak python program to give result in json

Hi , Below is the script which prints result in json but when i validate it has some tab or extra space issues. JSON result { "data": } This is the line I tweaked. Please advise. print "\t{", "\"{#NAME}\":\""+container+hn+"\"}" #!/usr/bin/env python # (2 Replies)
Discussion started by: ashokvpp
2 Replies

2. Shell Programming and Scripting

awk script to find time difference between HTTP PUT and HTTP DELETE requests in access.log

Hi, I'm trying to write a script to determine the time gap between HTTP PUT and HTTP DELETE requests in the HTTP Servers access log. Normally client will do HTTP PUT to push content e.g. file_1.txt and 21 seconds later it will do HTTP DELETE, but sometimes the time varies causing some issues... (3 Replies)
Discussion started by: Juha
3 Replies

3. Programming

Json dissect with python

hello, I'm trying to parse a json file and print out each element along with some related infos such type and value. I would like to do that in python and I found a nice support to quickly load the json file, instead I found a bit more difficult the information extraction. Example, I have... (0 Replies)
Discussion started by: Dedalus
0 Replies

4. Shell Programming and Scripting

http request

I am running a website but I still have problems with the "service temporarily unavailable error". I want to make a simple check if the website is up and running. Does anybody has an idea how to do it? (the site is password protected, so you have to add a user and pwd before logging in). ... (2 Replies)
Discussion started by: jurgen
2 Replies

5. Programming

C++ http GET request using sockets

Hello I am trying to communicate with a server that is ready to accept HTTP GET requests and send back data per the request. However, I have very little experience in socket programming and I don't really know how to debug this. Googling on the web hasn't yielded much, except people saying I... (2 Replies)
Discussion started by: flagman5
2 Replies

6. UNIX for Dummies Questions & Answers

cant make a http get request using wget

Hi all, Im trying to make an http get request to a web service from a linux machine like below and i get ERROR 500 wget http://10.1.21.236:8585/pns.asmx/Sen...&msgBody=werty 25018 $ --19:06:32-- http://10.1.21.236:8585/pns.asmx/Sen...erName=serverA Connecting to 10.1.21.236:8585...... (1 Reply)
Discussion started by: elthox
1 Replies

7. UNIX for Dummies Questions & Answers

HTTP request

Can anybody tell about http request processing in shell script..? (3 Replies)
Discussion started by: noufal
3 Replies

8. UNIX for Dummies Questions & Answers

http request forward

Hi, Maybe it's a stupid question, anyway here goes.. I have an Apache web server on Solaris box, let's say A, with a public ip and a web application on a Linux box, uhmmm B, on a private lan with a private ip. I want the people from outside to connect to the app, but its inside the lan,... (4 Replies)
Discussion started by: piltrafa
4 Replies

9. Shell Programming and Scripting

Http request in Linux

Hi, i need a guide how to write a script which i can do a http request. Let say the request look like below; http://www.test.com?txid=1&type=service&server=linux I have a list of "txid" (in *.txt) and need to run all "txid" acordingly. So that mean, every transaction i have to refer "txid"... (7 Replies)
Discussion started by: malaysoul
7 Replies

10. UNIX for Dummies Questions & Answers

unix script http request

Hi everybody, I have a *.vbs file which I want to run automatically. I want to know if there is anyway to implement the given example for e.g "http://255.255.255.55/script.vbs" what I mean is does anyone know how to make an http request from a unix script?? Thanks in advance!!!!!!!!!!! (1 Reply)
Discussion started by: arksal
1 Replies
Login or Register to Ask a Question