How would I implement the following curl command in Java URLConnection
curl -X PUT \
-H "X-Parse-Application-Id: " \
-H "X-Parse-REST-API-Key: " \
-H "Content-Type: application/json" \
-d '{"score":73453}'
Thanks in advance
Using the derived class of URLConnection which is HttpURLConnection you can easily do it.
URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
myURLConnection.setRequestMethod("PUT");
myURLConnection.setRequestProperty("X-Parse-Application-Id", "");
myURLConnection.setRequestProperty("X-Parse-REST-API-Key", "");
myURLConnection.setRequestProperty("Content-Type", "application/json");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
myURLConnection.connect();
JSONObject jsonParam = new JSONObject();
jsonParam.put("score", "73453");
OutputStream os = myURLConnection.getOutputStream();
os.write(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
os.close();
For curl -X GET \ -H "X-Parse-Application-Id: " \ -H "X-Parse-REST-API-Key: " \ -G \ --data-urlencode 'include=game
String charset = "UTF-8";
String query = String.format("include=%s", URLEncoder.encode("game", charset));
URL myURL = new URL(serviceURL+"?"+query);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
myURLConnection.setRequestMethod("GET");
myURLConnection.setRequestProperty("X-Parse-Application-Id", "");
myURLConnection.setRequestProperty("X-Parse-REST-API-Key", "");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
myURLConnection.connect();
Related
I use command "curl -X DELETE --header 'Accept: application/json' 'http://10.10.1.29:8181/onos/v1/flows/application/olsrflow' -u karaf:karaf"
It Can work.
but use JAVA Code does't work.
have any problem in my JAVA code?
URL dc0ContrailUrl2 = new URL("http://10.10.1.29:8181/onos/v1/flows/application/olsrflow");
HttpURLConnection dcConn2 = (HttpURLConnection) dc0ContrailUrl2.openConnection();
dcConn2.setDoOutput(true);
String login = "karaf:karaf";
String content = URLEncoder.encode (login) ;
String basicAuth = "Basic " + new String(new Base64().encode(login.getBytes()));
dcConn2.setRequestProperty("Authorization",basicAuth);
dcConn2.setRequestProperty("Content-Type", "application/json");
dcConn2.setRequestMethod("DELETE");
BufferedReader in2 = new BufferedReader(new InputStreamReader(dcConn2.getInputStream()));
String inputLine2;
while ((inputLine2 = in2.readLine()) != null){ //while response is not null, assign response to inputLine and print inputLine
System.out.println(inputLine2);
}
in2.close();
error HTTP response code: 415
You did not add Accept type the same as curl. Add the following line and remove the Content-Type:
dcConn2.setRequestProperty("Accept", "application/json");
Typically it means that server does not support MediaType has been passed. You have to figure out what does it expect and setup it in your request accordingly.
I would like to send a notification from my Java program to Firebase. But I don't know how to do that.
In order to send a notification using curl:
# api_key=YOUR_SERVER_KEY
# curl --header "Authorization: key=$api_key" \
--header Content-Type:"application/json" \
https://fcm.googleapis.com/fcm/send \
-d "{\"registration_ids\":[\"ABC\"]}"
I want the equivalent of that in Java. I tried the following but it is not correct
String rawData = "{\"registration_ids\":[\"ABC\"]}";
String encodedData = URLEncoder.encode( rawData, "UTF-8" );
URL u = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key="+api_key);
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
os.write(encodedData.getBytes());
Can someone help me write this as an httpPost in Java?
I just can't get it right
curl -X POST --user $YOUR_API_KEY_ID:$YOUR_API_KEY_SECRET \
-H "Accept: application/json" \
-H "Content-Type: application/json;charset=UTF-8" \
-d '{
"favoriteColor": "red",
"hobby": "Kendo"
}' \
"https://api.stormpath.com/v1/accounts/cJoiwcorTTmkDDBsf02bAb/customData"
Thanks in advance
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
URI uri = new URI("https://api.stormpath.com/v1/applications/7Jl082Y0Rsff2IBmtL8q2F/accounts");
httpPost.setURI(uri);
httpPost.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials(id, secret),
HTTP.UTF_8, false));
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type", "application/json");
httpPost.setEntity(new StringEntity(json[0].toString(), HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
I dont know if that helps ya out. Please someone help me write this curl command. =( ahah hthanks again
I am having trouble converting this to Java. So the curl line is
-d '{"skills":{"__op":"AddUnique","objects":["flying","kungfu"]}}' \
Normally what I do to a curl command such as this:
-d '{"score":73453}' \
would be:
httpRequest.setContent("{\"" +
"score\": 73452 "+
"}");
Not entirely sure what your issue actually is but a reasonably straight forward way to match that command in Java is this:
String url = "whatever.your.url.is";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection .setRequestProperty("Content-Type", "application/json");
connection .setRequestMethod("POST");
JSONObject json =new JSONObject();
JSONObject skills =new JSONObject();
skills.put("__op", "AddUnique");
skills.put("objects", new JSONArray(Arrays.asList("flying", "kungfu"));
json.put("skills": skills);
OutputStreamWriter wr= new OutputStreamWriter(connection.getOutputStream());
wr.write(json.toString());
How to post JSON data using HttpURLConnection? I am trying this:
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
StringReader reader = new StringReader("{'value': 7.5}");
OutputStream os = httpcon.getOutputStream();
char[] buffer = new char[4096];
int bytes_read;
while((bytes_read = reader.read(buffer)) != -1) {
os.write(buffer, 0, bytes_read);// I am getting compilation error here
}
os.close();
I am getting compilation error in line 14.
The cURL request is:
curl -H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "{'value': 7.5}" \
"a URL"
Is this the way to handle cURL request? Any information will be very helpful to me.
Thanks.
OutputStream expects to work with bytes, and you're passing it characters. Try this:
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
byte[] outputBytes = "{'value': 7.5}".getBytes("UTF-8");
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);
os.close();
You may want to use the OutputStreamWriter class.
final String toWriteOut = "{'value': 7.5}";
final OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
osw.write(toWriteOut);
osw.close();