주 콘텐츠로 건너뛰기
Example 생성 편집

Example: Various Programming Languages

shutterstock_1680857539-20231115-090656.jpg

Due to its characteristics, REST API is not platform-dependent. Therefore, it can be used in numerous programming languages other than the ones mentioned earlier, such as Python and VBA.

I will provide examples of performing the following REST API in several programming languages.

HTTP METHOD POST
URL baseURL/db/node
JSON BODY
{
    "Assign": {
        "1": {
            "X": 1,
            "Y": 2,
            "Z": 3
        }
    }
} 

 

C# - HttpClient

  • Example Code Snippet
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "baseURL/db/node");
request.Headers.Add("mapi-key", "your_api_key_hear");
var content = new StringContent("{\r\n    \"Assign\": {\r\n        \"1\": {\r\n            \"X\": 1,\r\n            \"Y\": 2,\r\n            \"Z\": 3\r\n        }\r\n    }\r\n}", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync()); 

 

JavaScript - Fetch

  • Example Code Snippet
var myHeaders = new Headers();
myHeaders.append("mapi-key", "your_api_key_hear");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
  "Assign": {
    "1": {
      "X": 1,
      "Y": 2,
      "Z": 3
    }
  }
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("baseURL/db/node", requestOptions)
  .then(response = response.text())
  .then(result = console.log(result))
  .catch(error = console.log('error', error)); 

 

Java - OkHttp

  • Example Code Snippet
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\r\n    \"Assign\": {\r\n        \"1\": {\r\n            \"X\": 1,\r\n            \"Y\": 2,\r\n            \"Z\": 3\r\n        }\r\n    }\r\n}");
Request request = new Request.Builder()
  .url("baseURL/db/node")
  .method("POST", body)
  .addHeader("mapi-key", "your_api_key_hear")
  .addHeader("Content-Type", "application/json")
  .build();
Response response = client.newCall(request).execute(); 

 

Dart - Dio

  • Example Code Snippet
var headers = {
  'mapi-key': 'your_api_key_hear',
  'Content-Type': 'application/json'
};
var data = json.encode({
  "Assign": {
    "1": {
      "X": 1,
      "Y": 2,
      "Z": 3
    }
  }
});
var dio = Dio();
var response = await dio.request(
  'baseURL/db/node',
  options: Options(
    method: 'POST',
    headers: headers,
  ),
  data: data,
);

if (response.statusCode == 200) {
  print(json.encode(response.data));
}
else {
  print(response.statusMessage);
} 

 

C - libcurl

  • Example Code Snippet
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(curl, CURLOPT_URL, "baseURL/db/node");
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "mapi-key: your_api_key_hear");
  headers = curl_slist_append(headers, "Content-Type: application/json");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  const char *data = "{\r\n    \"Assign\": {\r\n        \"1\": {\r\n            \"X\": 1,\r\n            \"Y\": 2,\r\n            \"Z\": 3\r\n        }\r\n    }\r\n}";
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl); 

 

NodeJS - Axios

  • Example Code Snippet
const axios = require('axios');
let data = JSON.stringify({
  "Assign": {
    "1": {
      "X": 1,
      "Y": 2,
      "Z": 3
    }
  }
});

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: '/db/node',
  headers: { 
    'mapi-key': '', 
    'Content-Type': 'application/json'
  },
  data : data
};

axios.request(config)
.then((response) = {
  console.log(JSON.stringify(response.data));
})
.catch((error) = {
  console.log(error);
}); 

 

0
컨텐츠가 도움이 되셨나요?