Nexon Isdumb
Nexon Isdumb

Reputation: 57

Convert Curl to Java

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 "+
           "}");

Upvotes: 2

Views: 667

Answers (1)

Ruairi O'Brien
Ruairi O'Brien

Reputation: 1229

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());

Upvotes: 1

Related Questions