Rubyでエンドポイントにアクセス

例「バーコードで物品管理」で作成した「バーコード指定でデータ取得」をするエンドポイントに対してRubyからアクセスします。

エンドポイントのページにサンプルコードがありますのでそれを使用します。

require 'net/https'                 <ここからテンプレートを使用>
require "json"

uri = URI.parse("https://dbaassandbox.matsuesoft.co.jp/????/proxyserver-AssetInfo-GetSpecificAssetByBarcode.php")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri.path)
req["Content-Type"] = "application/json"
req.body = JSON.generate({
    "TOKEN"=> "????",
    "param_AssetInfo_Barcode_where"=> "9784845623556"             ← 取得したいバーコードの値を設定
})
res = http.request(req)
if res.code != "200" then
    # Error Occured
else
    # Success
    json_response = JSON.parse(res.body)
    if json_response["_status"] == "OK" then
        # Success. Result is in: json_response["Result"]             <ここまでテンプレートを使用>
        ↓ここを追加
        if json_response["Result"] then
            print "Result - Item Name: " + json_response["Result"]["ItemName"] + "\n";       ← 取得結果を表示
            print "Result - Description: " + json_response["Result"]["Description"] + "\n";  ← 取得結果を表示
            print "Result - Barcode: " + json_response["Result"]["Barcode"] + "\n";          ← 取得結果を表示
        else
            print "No Item matched\n"
        end
        ↑ここまで追加
    else              <ここからテンプレートを使用>
        print "Error! Result: " + json_response["_status"] + " : " + json_response["Message"] + "\n";
    end
end      <ここまでテンプレートを使用>

実行しますと以下のように表示されます。

Result - Item Name: DEF Travel Book
Result - Description: This is DEF...
Result - Barcode: 9784845623556

※Windows版Rubyの場合はSSL関係で設定が必要な場合があります。

戻る

ログイン当サイトのユーザ名(ID)でログイン
クイックログイン:Facebookのアカウントで即時ログイン(ログイン情報保持オプション:ON) / 新規登録


お問い合わせ

© 2016-2024 Matsuesoft Corporation