API connect による Get Curl SDK 化 の例


# curl
curl --request GET \
  --url https://***/api/test01 \
  --header 'accept: application/json' \
  --header 'x-ibm-client-id: default'

#Java

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url("https://***/api/test01")
  .get()
  .addHeader("x-ibm-client-id", "default")
  .addHeader("accept", "application/json")
  .build();

Response response = client.newCall(request).execute();
200
A successful invocation response
200

#PHP

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://***/api/test01",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "x-ibm-client-id: default"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

# Ruby

require 'uri'
require 'openssl'
require 'net/http'

url = URI("https://***/api/test01")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

request = Net::HTTP::Get.new(url)
request["x-ibm-client-id"] = 'default'
request["accept"] = 'application/json'

response = http.request(request)
puts response.read_body


#Python

import http.client

conn = http.client.HTTPSConnection("service.us.apiconnect.ibmcloud.com")

headers = {
    'x-ibm-client-id': "default",
    'accept': "application/json"
    }

conn.request("GET", "***/api/test01", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

#Go


package main

import (
	"fmt"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://***/api/test01"

	req, _ := http.NewRequest("GET", url, nil)

	req.Header.Add("x-ibm-client-id", "default")
	req.Header.Add("accept", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}

#Node.js

// Install request by running "npm install --save request"
var request = require("request");

var options = { method: 'GET',
  url: 'https://***/api/test01',
  headers: { accept: 'application/json', 'x-ibm-client-id': 'default' } };

request(options, function (error, response, body) {
  if (error) return console.error('Failed: %s', error.message);

  console.log('Success: ', body);
});

#Swift

import Foundation

let headers = [
  "x-ibm-client-id": "default",
  "accept": "application/json"
]

var request = NSMutableURLRequest(URL: NSURL(string: "https://***/api/test01")!,
                                        cachePolicy: .UseProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.HTTPMethod = "GET"
request.allHTTPHeaderFields = headers

let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? NSHTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()