JSON parsing을 위해 Gson 라이브러리를 사용하여 제공하고 있습니다. Gson 라이브러리에 대한 자세한 설명은 https://github.com/google/gson 에서
확인 하실 수 있습니다.
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
public class Example {
static public void main ( String[] args ) {
String openApiURL = "http://aiopen.etri.re.kr:8000/ParaphraseQA";
String accessKey = "YOUR_ACCESS_KEY"; // 발급받은 API Key
String sentence1 = "YOUR_SENTENCE_1";
String sentence2 = "YOUR_SENTENCE_2";
Gson gson = new Gson();
Map<String, Object> request = new HashMap<>();
Map<String, String> argument = new HashMap<>();
argument.put("sentence1", sentence1);
argument.put("sentence2", sentence2);
request.put("argument", argument);
URL url;
Integer responseCode = null;
String responBody = null;
try {
url = new URL(openApiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Authorization", accessKey);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(gson.toJson(request).getBytes("UTF-8"));
wr.flush();
wr.close();
responseCode = con.getResponseCode();
InputStream is = con.getInputStream();
byte[] buffer = new byte[is.available()];
int byteRead = is.read(buffer);
responBody = new String(buffer);
System.out.println("[responseCode] " + responseCode);
System.out.println("[responBody]");
System.out.println(responBody);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<?php
$openApiURL = "http://aiopen.etri.re.kr:8000/ParaphraseQA";
$accessKey = "YOUR_ACCESS_KEY";
$sentence1 = "YOUR_SENTENCE_1";
$sentence2 = "YOUR_SENTENCE_2";
$request = array(
"argument" => array (
"sentence1" => $sentence1,
"sentence2" => $sentence2,
)
);
try {
$server_output = "";
$ch = curl_init();
$header = array(
"Content-Type:application/json; charset=UTF-8",
"Authorization":{$accessKey}
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $openApiURL);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode ( $request) );
$server_output = curl_exec ($ch);
if($server_output === false) {
echo "Error Number:".curl_errno($ch)."\n";
echo "Error String:".curl_error($ch)."\n";
}
curl_close ($ch);
} catch ( Exception $e ) {
echo $e->getMessage ();
}
echo "result = " . var_dump($server_output);
?>
JSON parsing을 위해 jsoncpp 라이브러리를 사용하여 제공하고 있습니다. jsoncpp 라이브러리에 대한 자세한 설명은 https://github.com/open-source-parsers/jsoncpp 에서 확인 하실 수 있습니다.
HTTP 통신을 위해 curl 라이브러리를 사용하여 제공하고 있습니다. curl 라이브러리에 대한 자세한 설명은 https://curl.haxx.se/libcurl 에서 확인 하실 수 있습니다.
컴파일을 위해서는 아래와 같이 추가된 LIB에 대한 옵션을 추가해야 합니다.
g++ (c++파일명) (JSONCPP)/json/json.h (JSONCPP)/json/json-forwards.h (JSONCPP)/jsoncpp.cpp -I(CURL)/include -lcurl
#include <curl/curl.h>
#include <json/json.h>
#include <iostream>
#include <string>
using namespace std;
size_t writeDataFunc(void *ptr, size_t size, size_t nmemb, std::string stream);
int main() {
char* openApiURL = (char*)"http://aiopen.etri.re.kr:8000/ParaphraseQA";
string accessKey = "YOUR_ACCESS_KEY";
string sentence1 = "YOUR_SENTENCE_1";
string sentence2 = "YOUR_SENTENCE_2";
Json::Value request;
Json::Value argument;
argument["sentence1"] = sentence1;
argument["sentence2"] = sentence2;
request["argument"] = argument;
CURL *curl;
curl_slist* responseHeaders = NULL;
curl = curl_easy_init();
if( curl == NULL ) {
cout << "Unable to initialize cURL interface" << endl ;
} else {
responseHeaders = curl_slist_append( responseHeaders , "Content-Type: application/json; charset=UTF-8" ) ;
responseHeaders = curl_slist_append( responseHeaders , ("Authorization: " + accessKey).c_str() ) ;
string requestJson = request.toStyledString();
long statusCode;
string response;
curl_easy_setopt(curl, CURLOPT_URL, openApiURL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, responseHeaders ) ;
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestJson.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeDataFunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode);
curl_easy_cleanup(curl);
cout << "[responseCode] " << statusCode << endl;
cout << "[responBody]" << endl;
cout << response << endl;
}
return 0;
}
size_t writeDataFunc(void *ptr, size_t size, size_t nmemb, std::string stream) {
size_t realsize = size * nmemb;
std::string temp(static_cast<const char*>(ptr), realsize);
stream.append(temp);
return realsize;
}
python 3.0을 기준으로 작성되었습니다.
HTTP 통신을 위해 urllib3 라이브러리를 사용하여 제공하고 있습니다. Python 3.0 이하의 버전에서 예제를 실행하기 위해서는 별도로 urllib3의 설치가 필요합니다. 설치에 대한 설명은 https://pypi.python.org/pypi/urllib3 를 참고하시기 바랍니다. urllib3 라이브러리에 대한 자세한 설명은 https://urllib3.readthedocs.io/en/latest/ 에서 확인 하실 수 있습니다.
#-*- coding:utf-8 -*-
import urllib3
import json
openApiURL = "http://aiopen.etri.re.kr:8000/ParaphraseQA"
accessKey = "YOUR_ACCESS_KEY"
sentence1 = "YOUR_SENTENCE_1"
sentence2 = "YOUR_SENTENCE_2"
requestJson = {
"argument": {
"sentence1": sentence1 ,
"sentence2": sentence2
}
}
http = urllib3.PoolManager()
response = http.request(
"POST",
openApiURL,
headers={"Content-Type": "application/json; charset=UTF-8","Authorization" : accessKey},
body=json.dumps(requestJson)
)
print("[responseCode] " + str(response.status))
print("[responBody]")
print(str(response.data,"utf-8"))
var openApiURL = 'http://aiopen.etri.re.kr:8000/ParaphraseQA';
var access_key = 'YOUR_ACCESS_KEY';
var sentence1 = 'YOUR_SENTENCE_1';
var sentence2 = 'YOUR_SENTENCE_2';
var requestJson = {
'argument': {
'sentence1 ': sentence1 ,
'sentence2 ': sentence2
}
};
var request = require('request');
var options = {
url: openApiURL,
body: JSON.stringify(requestJson),
headers: {'Content-Type':'application/json,'Authorization':access_key}
};
request.post(options, function (error, response, body) {
console.log('responseCode = ' + response.statusCode);
console.log('responseBody = ' + body);
});