Русский | English

Пример обращения к RESTful API на Ruby:

require 'net/http'
require 'json'
USER_TOKEN = '?????????????????'
HOST = 'https://ws-dss.com'
# Job creation preparing

uri = URI.parse(HOST + "/ws_jobs.json")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true

req = Net::HTTP::Post.new(uri)
req['user-token'] = USER_TOKEN
req.set_form_data('ws_job[ws_method_id]' => 23, 'ws_job[input]' => '["AEC01000"]')
res = https.request(req)
if !res.is_a?(Net::HTTPSuccess) # error check
puts "Error #{res}\n#{res.body}"
exit(1)
end
h = JSON.parse(res.body)
ws_job_id = h["id"]
puts "Job was created! id = #{ws_job_id}"
i = 0
# if job was not completed we must wait
while h["output"].nil? and i < 100 do
i += 1
puts i
sleep(i*5)
uri = URI.parse(HOST + "/ws_jobs/#{ws_job_id}.json")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Get.new(uri)
req['user-token'] = USER_TOKEN
res = https.request(req)
if !res.is_a?(Net::HTTPSuccess)
puts "Error #{res}\n#{res.body}"
exit(1)
end
h = JSON.parse(res.body)
end
puts "i = #{i} output =\n#{h["output"]}"

Пример обращения к RESTful API на VBA в Excel:

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = Range("ws_job")
    objHTTP.Open "PATCH", URL, False
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.setRequestHeader "user-token", Range("token")
    objHTTP.send ("ws_job[input]=" + s)
    s = objHTTP.responseText
    
    Application.Wait Now + #12:00:03 AM#
    
    objHTTP.Open "GET", URL, False
    objHTTP.setRequestHeader "user-token", Range("token")
    objHTTP.send
    s = objHTTP.responseText
    
    Set Json = JsonConverter.ParseJson(s)
    
    a = Split(Json("output"), vbLf)
    i = 1
    For Each r In a
        Worksheets("trace").Cells(i, 1) = r
        i = i + 1
    Next r

Пример обращения к RESTful API из JavaScript:

  var req = new XMLHttpRequest();
  var path = "https://ws-dss.com/ws_jobs.json";
  req.onreadystatechange=state_change;
  req.open("GET", path, true);
  req.setRequestHeader("user-token","?????????????");
  req.send(null);

  function state_change()
  {
  if (req.readyState==4)
    {// 4 = "loaded"
    if (req.status==200)
      {// 200 = OK
      // ...our code here...
      alert('ok');
      }
    else
      {
      alert("Problem retrieving data");
      }
    }
  }