About API

Data exchange between open data portal and user is carried out over the HTTPS protocol. Parameters transmitted to the services during each application are mainly implemented using the Get method in the following way:

https://api.opendata.az/{version}/{format}/{name of organization}/{name of service}
{version}- indicates the version of the service (v1, v2, etc.).
{format} - indicates the type of service (JSON, XML, etc.).
{name of institution} - indicates the institution or group to which the service belongs.
{service name} - defines the name of the method that calls the service.
If JSON format is selected as the format, the response returned from the service will be in JSON format and returns a certain set of data according to the structure of each service.
Example 1:
Probably the response will be as following:
{
"Status": "OK",
"StatusMessage": null,
"Response": [
{
...
}
]
}
Status = { “ OK”, “InputError”, “ServerError” }

Comprehensive information on API

If JSON format is selected, the response returned from the service will be in JSON format and it returns a certain set of data according to the structure of each service.
Example 1:
Probably the response will be as following:
{
"Status": "OK",
"StatusMessage": null,
"Response": [
{
...
}
]
}
Status will be probably as follows:
Status = {“ OK”, “InputError”, “ServerError” }
Status =”OK” – service returns usual data. StatusMessage box is null.
Status =”InputError” – input is wrong. StatusMessage input box returns different message for each service.
Status =”ServerError” - error code is indicated in StatusMessage box. 
Response – information from service is indicated.
Example 2:
Response from service:
"Status": "OK",
"StatusMessage": null,
"Response": {
"Header": {
"Atms": {
"Atm": {
"AtmNumber": {
"Name": "№"
},
"AtmCode": {
"Name": "ATM Code"
},
"AtmName": {
"Name": "Name"
},
"AtmAddr": {
"Name": "Address"
},
"AtmCity": {
"Name": "City"
},
"Name": "ATM"
},
"Name": "list of ATMs"
}
},
"Body": {
"ATMS": {
"ATM": [
{
"AtmNumber": "18",
"AtmCode": "ATM 00918",
"AtmName": "Yasamal TKN",
"AtmAddr": " 239 A.M.Sharifzadeh ",
"AtmCity": "Baku"
},
....
]
}
},
"Name": "The list of AzerPost ATMs  (№, ATM Code, Name, Address, City)"
}
}
Probably status will be as follows:
Status = {“ OK”, “InputError”, “ServerError” }.
Status =”OK” – service returns usual data. StatusMessage is null.
Status =” InputError” – input is wrong. StatusMessage box returns different message for each service.
Status =”ServerError” – error code from server is indicated in StatusMessage box.
Response – data returned from service is indicated. 
Name – name of service
Header – indicates heading of data (name describing the data) according to the response from service.
Body
- main data returned from service is indicated.
 

Examples of code

// Getting JSON string by GET method in C# programming language:
string GET(string url) 
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    try {
        WebResponse response = request.GetResponse();
        using (Stream responseStream = response.GetResponseStream()) {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            return reader.ReadToEnd();
        }
    }
    catch (WebException ex) {
        WebResponse errorResponse = ex.Response;
        using (Stream responseStream = errorResponse.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            String errorText = reader.ReadToEnd();
            // logging of error
        }
        throw;
    }
}

// C# JSON string is POST in C# programming language
void POST(string url, string jsonContent) 
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    Byte[] byteArray = encoding.GetBytes(jsonContent);

    request.ContentLength = byteArray.Length;
    request.ContentType = @"application/json";

    using (Stream dataStream = request.GetRequestStream()) {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }
    long length = 0;
    try {
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
            length = response.ContentLength;
        }
    }
    catch (WebException ex) {
        // logging of error
    }
}

// Getting JSON string by GET method on Android
public String readJSON(String URL) {
    StringBuilder stringBuilder = new StringBuilder();
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();
        } else {
            Log.d("JSON", "uploading of file is failed");
        }
    } catch (Exception e) {
        Log.d("readJSON", e.getLocalizedMessage());
    }        
    return stringBuilder.toString();
}

// Getting JSON by GET method on iOS Swift
typealias ServiceResponse = (JSON, NSError?) -> Void
 
class RestApiManager: NSObject {
    static let sharedInstance = RestApiManager()
 
    let baseURL = "add json URL"
 
    func getData(onCompletion: (JSON) -> Void) {
        let route = baseURL
        makeHTTPGetRequest(route, onCompletion: { json, err in
            onCompletion(json as JSON)
        })
    }
 
    func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
        let request = NSMutableURLRequest(URL: NSURL(string: path)!)
 
        let session = NSURLSession.sharedSession()
 
        let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            let json:JSON = JSON(data: data)
            onCompletion(json, error)
        })
        task.resume()
    }
}