MIDAS API Addon MIDAS API: Code Samples

Below are some example code fragments for common programming languages of how you could access the MIDAS API via HTTP Post requests.
In all of the following examples the "get_setting" API call is made in order to retrieve the current version of MIDAS.

Jump to language:

C#
using(WebClient client = new WebClient())
{
    System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
    parameters.Add("key", "your_api_key");
    parameters.Add("action", "get_setting");
    parameters.Add("setting", "version");
    byte[] responsebytes = client.UploadValues("https://your_midas_url/api.pl", "POST", parameters);
    string response = Encoding.UTF8.GetString(responsebytes);
}
Go
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
    "net/http"
)

func main() {
    values := map[string]string{"key":"your_api_key","action":"get_setting","setting":"version"}
    json_data, err := json.Marshal(values)
    if err != nil {
        log.Fatal(err)
    }
    resp, err := http.Post("https://your_midas_url/api.pl", "application/json",
        bytes.NewBuffer(json_data))
    if err != nil {
        log.Fatal(err)
    }
    var res map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&res)
    fmt.Println(res["json"])
}
Java
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://your_midas_url/api.pl");

List<NameValuePair> params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("key", "your_api_key"));
params.add(new BasicNameValuePair("action", "get_setting"));
params.add(new BasicNameValuePair("setting", "version"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

if (entity != null) {
    InputStream instream = entity.getContent();
    try {
        // process response
    } finally {
        instream.close();
    }
}
jQuery
$.post("https://your_midas_url/api.pl", { key: "your_api_key", action: "get_setting", setting: "version" })
.done(function(response) {
 alert("Response: " + response);
}, "JSON");
.NET
using System;
using System.Collections.Specialized;
using System.Net;

public static class Http
{
    public static byte[] Post(string uri, NameValueCollection pairs)
    {
        byte[] response = null;
        using (WebClient client = new WebClient())
        {
            response = client.UploadValues(uri, pairs);
        }
        return response;
    }
}
var response = Http.Post("https://your_midas_url/api.pl", new NameValueCollection() {
    { "key", "your_api_key" },
    { "action", "get_setting" },
    { "setting", "version" }
});
Perl
use LWP::UserAgent;
my$ua = LWP::UserAgent->new(env_proxy => 0,keep_alive => 0,timeout => 30,agent =>'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.0)');
my$request = $ua->post("https://your_midas_url/api.pl", [key => "your_api_key", action => "get_setting",setting=>"version"]);
if ($request->is_success) {
	$response=$request->content;
}
PHP
$parameters = "key=your_api_key&action=get_setting&setting=version";
$ch = curl_init("https://your_midas_url/api.pl");
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
Python
import urllib
import urllib2
parameters = {'key' : 'your_api_key',
          'action' : 'get_setting',
          'setting' : 'version' }
data = urllib.urlencode(parameters)
req = urllib2.Request("https://your_midas_url/api.pl", data)
getresponse = urllib2.urlopen(req)
response = getresponse.read()
 
Ruby
require "net/http"
require "uri"

uri = URI.parse("https://your_midas_url/api.pl")

response = Net::HTTP.post_form(uri, {"key" => "your_api_key", "action" => "get_setting", "setting" => "version"})