Reputation: 453
I have this curl call
curl -d "variable1=text1&variable2=text2&variable3=text3" -d information='{"id":"1234567489", "token":"AAACSweqPLFPrf8F6r1sux2AZDZD"}' https://localhost:8080/token
And I'm trying to convert it into a Java call, but I don't know how to manage the 2 types of fields (url encoded and json)
Any ideas?
Thanks
Upvotes: 0
Views: 658
Reputation: 453
I actually figured it out, just needed to URL-Encode the JSON part of the call and then I was able to make the call just with an entire url-encoded body using the regular Apache HTTP Client.
Upvotes: 0
Reputation: 467
If you are trying to do URL/HTTP stuff in Java you will want to look into Apache HTTP Client Library.
If you are trying to parse JSON then you will want the JSON libraries. Alternatively if you want get your hands dirty you can make use of java.net.URL and/or java.net.URLConnection:
URL url = new URL("http://stackoverflow.com");
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException ignore) {}
}
Also see Oracle's simple tutorial on the subject. It's however a bit verbose.
Swift
Upvotes: 2