I am trying to execute a curl command and get a part of the response as a string. The command that I am trying to execute is as follows
curl -X POST "https://kakaoapi.aligo.in/akv10/token/create/30/s/" \
--data-urlencode "apikey=xxxxx" \
--data-urlencode "userid=xxxxx"
The response that I get when I execute this in the terminal is as follows
{"code":0,"message":"\uc815\uc0c1\uc801\uc73c\ub85c \uc0dd\uc131\ud558\uc600\uc2b5\ub2c8\ub2e4.","token":"tokenvalue","urlencode":"urlencoded"}%
The java code that I am trying to execute to get this result is something like this
String command =
"curl -X POST \"https://kakaoapi.aligo.in/akv10/token/create/30/s/\" \\\n"
"\t--data-urlencode \"apikey=xxxxx\" \\\n"
"\t--data-urlencode \"userid=xxxxx\"";
Process pr = Runtime.getRuntime().exec(command);
String result = new BufferedReader(
new InputStreamReader(pr.getInputStream()))
.lines()
.collect(Collectors.joining("\n"));
log.info(result);
I am currently getting a null value in the log.
It would be greatly appreciated if anyone could tell me what I am doing wrong, or suggest a better way of doing this.
I have tried doing this by creating a httpclient and sending a post request, and I did get a response but the authentication failed for some reason.
Thank you in advance!
CodePudding user response:
A better way of doing this is by using Unirest library (http://kong.github.io/unirest-java):
Unirest.setTimeouts(0, 0);
HttpResponse<String> response = Unirest.post("https://kakaoapi.aligo.in/akv10/token/create/30/s/?apikey=xxxxx&userid=xxxxx")
.asString();
The first step is to install it's maven / gradle dependency and then try the code above.