Calling third party REST API with curl in Java - java

I want to call this curl command to get list of applicant names from Java in JSON
curl -u uname:pass my_REST_Endpoint_provided_by_vendor
here is my code:
URL myURL = new URL("url");
HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(unamepass.getBytes("UTF-8"));
conn.setRequestProperty ("Authorization", basicAuth);
int code = conn.getResponseCode(); // 200 = HTTP_OK
System.out.println("Response (Code):" + code);
System.out.println("Response (Message):" + conn.getResponseMessage());
If I run this command on my command prompt it runs fine and gives me the output but if I run this code I get Response (Code):405
java.io.IOException: Server returned HTTP response code: 405 for URL:
Where am I going wrong?

You are getting an 405 error because you are using the HTTP Method PUT instead of GET, which is used by curl by default. Remove the line:
conn.setRequestMethod("PUT");

Related

Java - HttpURLConnection PUT request with empty body

I'm trying make a request with Java, when I call it using cURL like this, it works:
curl -X PUT http://serverurl.com/method/6eb276a2-5c79-4f6e-a4b5-a26b0e6848c7/action -H 'Content-Type: application/json' -H 'Token: cba5f12c-af55-480f-970e-525e446ef153' -H 'Content-Length : 0'
If I call the same request without passing header Content-Length param, I get 411 HTTP error, length required.
This is my code in Java:
URL url = new URL("http://serverurl.com/method/6eb276a2-5c79-4f6e-a4b5-a26b0e6848c7/action");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("PUT");
con.addRequestProperty("Content-Type", "application/json");
con.addRequestProperty("Token", "cba5f12c-af55-480f-970e-525e446ef153");
con.connect();
This request is getting a 411 HTTP code response. So, I tryed to add:
con.addRequestProperty("Content-Length", "0");
But it doesn't work, so I changed to:
URL url = new URL("http://serverurl.com/method/6eb276a2-5c79-4f6e-a4b5-a26b0e6848c7/action");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("PUT");
con.addRequestProperty("Content-Type", "application/json");
con.addRequestProperty("Token", "cba5f12c-af55-480f-970e-525e446ef153");
con.setDoOutput(true);
con.getOutputStream().close();
con.setFixedLengthStreamingMode(0);
con.connect();
But now I'm getting 400 HTTP code.
How can I do a PUT request with an empty body and setting content length to match the cURL call?
using the HttpUrlConnection, you should use the setRequestProperty method to add headers to your request. I can see your using the "addRequestProperty" which is probably why its not working. But refer to this link for more info https://juffalow.com/java/how-to-send-http-get-post-request-in-java and heres some code that i use to for a put request
URL url = new URL(BASE_URL+"/"+userID+".json");
urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setDoOutput(true);
urlRequest.setRequestMethod("PUT");
urlRequest.setRequestProperty("Content-Type", "application/json;
charset=UTF-8");
OutputStream os = urlRequest.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("{\"idToken\":\""+"token"+"\"}");
osw.flush();
osw.close();
urlRequest.connect();
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream)
urlRequest.getContent()));//Convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject();//Maybe an array or object
well thats just sample what i use... and i hope this works for you. Happy coding.

How to wait for Expect 100-continue response in Java using HttpURLConnection

I am stuck using HttpURLConnection to make a PUT http request to a web-server. I have some code that will make a PUT request just fine, and I can trivially include the 'Expect 100-continue Request Property' in the headers however try as I might I can't seem to make the function wait for the '100 Continue' response from the server before sending the actual http payload.
I get the following (from Wireshark)
PUT /post/ HTTP/1.1
User-Agent: curl/7.35.0
Accept: */*
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Host: somerandomdomain.info
Connection: keep-alive
Content-Length: 17
Some data for you
HTTP/1.1 100 Continue
...rest of web-server response...
I'm sure I am missing something obvious however after googling I have drawn a blank - can anyone help?
Many thanks if so :)
Http PUT code snippet below:
String url = "http://somerandomdomain.info";
String postJsonData = "Some data for you\n";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// Setting basic post request
con.setRequestMethod("PUT");
con.setRequestProperty("User-Agent", "jcurl/7.35.0");
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Content-Length", postData.length() + "");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Expect", "100-continue");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postData);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post Data : " + postData);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
//printing result from response
System.out.println(response.toString());
It's not obvious to me why they designed it this way, but the code that implements the Expect:100 logic is only used if you have called one of setFixedLengthStreamingMode(int contentlen) or the overload for long or setChunkedStreamingMode(int chunklen) before doing getOutputStream. In this case I recommend the first as simplest.

getting filenotfoundexception while sending json data using java

I am trying to send json data to Influx db using following code:
String url = "http://xx.x.xx.xx:8086/db/monitoring/check_1113?u=root&p=root";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
//String userpass = "user" + ":" + "pass";
//String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes("UTF-8"));
//conn.setRequestProperty ("Authorization", basicAuth);
//String data = "{\"format\":\"json\",\"pattern\":\"#\"}";
System.out.println("Data to send: "+"[{\"name\": \"check_222\",\"columns\": [\"time\", \"sequence_number\", \"value\"],\"points\": [["+unixTime+", 1, \"122\"]]}]");
String data = "[{\"name\": \"check_333\",\"columns\": [\"time\", \"sequence_number\", \"value\"],\"points\": [["+14444444444+", 1, \"122\"]]}]";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();
new InputStreamReader(conn.getInputStream());
System.out.println("Data Sent");
Where xx.xx.xx.xx is the ip of server where influx is deployed and i am using the Ip.
When i do a manual curl with this data (on localhost), the data is sent successfully. curl is provided below:
curl -X POST -d '[{"name": "check_223","columns": ["time", "sequence_number", "value"],"points": [[1445271004000,1,70.8880519867]]}]' 'http://localhost:8086/db/monitoring/series?u=root&p=root'
But when I run the code to send the data via the java program shared above, i get following error:
java.io.FileNotFoundException: http://xx.x.xx.xx:8086/db/monitoring/check_1113?u=root&p=root
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1834)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at com.snapdeal.hadoop.monitoring.hdfs1.App.sendJsonDataToInflux(App.java:52)
at com.snapdeal.hadoop.monitoring.hdfs1.App.main(App.java:89)
[INFO - 2015-10-20T16:27:13.152Z] ShutdownReqHand - _handle - About to shutdown
And to add to it, I am using phantomJS to get the data from web page and pass that data in the JSON request. But for simplicity I have hard-coded it at present.
This should be relatively obvious. A 405 indicates that the HTTP Method on the request is not supported by the endpoint. The service you are calling does not support a PUT method.

Java cURL alternative with HttpURLConnection

I was asked to port a PHP module I was writing to Java. I was previously using PHP's native cURL library, now trying to achieve the same action with HttpURLConnection.
Here's the call I want to do with cURL:
curl -u 'ExactID:Password' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'Accept: application/json' \
-d '{
"transaction_type":"00",
"amount":"15.75",
"cardholder_name":"PaulTest",
"transarmor_token":"3000",
"credit_card_type":"Visa",
"cc_expiry":"0016",
}' \
https://api.demo.globalgatewaye4.firstdata.com/transaction/v11
Here's what I have in Java, which returns a HTTP 400 error. Any ideas?
public static void main(String[] args) {
URL url = new URL("https://api.demo.globalgatewaye4.firstdata.com/transaction/v11");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
String userpass = "ExactID" + ":" + "Password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
conn.setRequestProperty ("Authorization", basicAuth);
JSONObject obj = new JSONObject();
obj.put("transaction_type", "00");
obj.put("amount", "10");
obj.put("cardholder_name", "PaulTest");
obj.put("transarmor_token", "3000");
obj.put("cc_expiry", "0016");
obj.put("credit_card_type", "Visa");
String input = obj.toString();
System.out.println(input);
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode() + conn.getResponseMessage());
}
One ambiguity in your java code is on string to byte array encoding. By default java will use your default platform encoding, but it's a good practice to express it explicitly because it often lead to hard to track bug
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes("ISO-8859-1")));
To be sure also check the encoded base 64 value generated by java on curl by using
-H 'Authorization: Basic ....`
Instead of -u
Also I'd try to cast the created URLConnection to HttpsURLConnection. Thay may/not make difference
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
After tinkering around, I made two mistakes:
For this POST method, basic authentication was not required. The user & pw goes into the JSON body along with the other parameters.
Also, my "transarmor_token" field needed to be 16 digits.
Conclusion: HttpURLConnection is a great cURL alternative. Forget about using the curl-java binding.
Thanks!

HttpURLConnection sends a POST request even though httpCon.setRequestMethod("GET"); is set

Here is my code:
String addr = "http://172.26.41.18:8080/domain/list";
URL url = new URL(addr);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
httpCon.setUseCaches(false);
httpCon.setAllowUserInteraction(false);
httpCon.setRequestMethod("GET");
httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463");
httpCon.connect();
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
out.close();
What I see in response:
500 Server error
I open my httpCon var, and what I see:
POST /rest/platform/domain/list HTTP/1.1
Why is it set to POST even though I have used httpCon.setRequestMethod("GET"); to set it to GET?
The httpCon.setDoOutput(true); implicitly set the request method to POST because that's the default method whenever you want to send a request body.
If you want to use GET, remove that line and remove the OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); line. You don't need to send a request body for GET requests.
The following should do for a simple GET request:
String addr = "http://172.26.41.18:8080/domain/list";
URL url = new URL(addr);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setUseCaches(false);
httpCon.setAllowUserInteraction(false);
httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463");
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
See also:
Using java.net.URLConnection to fire and handle HTTP requests
Unrelated to the concrete problem, the password part of your Authorization header value doesn't seem to be properly Base64-encoded. Perhaps it's scrambled because it was examplary, but even if it wasn't I'd fix your Base64 encoding approach.

Categories