MainframeMaster

COBOL Tutorial

COBOL API Integration

Progress0 of 0 lessons

Introduction to API Integration in COBOL

API integration in COBOL enables modern applications to communicate with external systems, web services, and cloud platforms. This capability is essential for building contemporary enterprise applications that need to interact with REST APIs, microservices, and cloud-based systems.

Modern COBOL supports various integration methods:

  • REST API Integration: HTTP-based communication with RESTful services
  • JSON Processing: Handling JSON data structures and responses
  • Web Services: SOAP and REST web service consumption
  • HTTP Communication: Direct HTTP client operations
  • Cloud Integration: Connecting to cloud platforms and services

JSON Processing in COBOL

JSON processing is fundamental to modern API integration. COBOL provides intrinsic functions for parsing and generating JSON data.

Basic JSON Processing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
IDENTIFICATION DIVISION. PROGRAM-ID. JSON-PROCESSING-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 JSON-DATA. 05 JSON-STRING PIC X(500). 05 JSON-LENGTH PIC 9(4). 01 CUSTOMER-DATA. 05 CUSTOMER-ID PIC X(10). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-EMAIL PIC X(50). 05 CUSTOMER-BALANCE PIC S9(7)V99. 01 JSON-RESPONSE PIC X(1000). PROCEDURE DIVISION. PERFORM INITIALIZE-JSON-DATA PERFORM PARSE-JSON-DATA PERFORM GENERATE-JSON-RESPONSE STOP RUN. INITIALIZE-JSON-DATA. MOVE '{"customerId":"CUST001","customerName":"John Doe",' TO JSON-STRING(1:50) MOVE '"customerEmail":"john.doe@email.com","customerBalance":1250.75}' TO JSON-STRING(51:60) MOVE 110 TO JSON-LENGTH. PARSE-JSON-DATA. JSON PARSE JSON-STRING INTO CUSTOMER-DATA ON EXCEPTION DISPLAY "JSON parsing error occurred" NOT ON EXCEPTION DISPLAY "JSON parsed successfully" DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Customer Name: " CUSTOMER-NAME DISPLAY "Customer Email: " CUSTOMER-EMAIL DISPLAY "Customer Balance: $" CUSTOMER-BALANCE END-JSON. GENERATE-JSON-RESPONSE. JSON GENERATE JSON-RESPONSE FROM CUSTOMER-DATA ON EXCEPTION DISPLAY "JSON generation error occurred" NOT ON EXCEPTION DISPLAY "Generated JSON: " JSON-RESPONSE END-JSON.

REST API Integration

REST API integration allows COBOL applications to communicate with modern web services using standard HTTP methods.

HTTP Client Operations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
IDENTIFICATION DIVISION. PROGRAM-ID. REST-API-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 API-CONFIGURATION. 05 API-BASE-URL PIC X(100) VALUE "https://api.example.com". 05 API-ENDPOINT PIC X(50). 05 API-KEY PIC X(50) VALUE "your-api-key-here". 01 HTTP-REQUEST. 05 REQUEST-METHOD PIC X(10). 05 REQUEST-HEADERS PIC X(200). 05 REQUEST-BODY PIC X(1000). 05 RESPONSE-CODE PIC 9(3). 05 RESPONSE-BODY PIC X(2000). 01 CUSTOMER-REQUEST. 05 CUSTOMER-ID PIC X(10). 05 REQUEST-TYPE PIC X(10). PROCEDURE DIVISION. PERFORM INITIALIZE-API-CONFIG PERFORM DEMONSTRATE-GET-REQUEST PERFORM DEMONSTRATE-POST-REQUEST STOP RUN. INITIALIZE-API-CONFIG. MOVE "/customers" TO API-ENDPOINT STRING "Authorization: Bearer " DELIMITED BY SIZE API-KEY DELIMITED BY SIZE INTO REQUEST-HEADERS. DEMONSTRATE-GET-REQUEST. DISPLAY "=== GET Request Demo ===" MOVE "GET" TO REQUEST-METHOD MOVE "CUST001" TO CUSTOMER-ID STRING API-BASE-URL DELIMITED BY SIZE API-ENDPOINT DELIMITED BY SIZE "/" DELIMITED BY SIZE CUSTOMER-ID DELIMITED BY SIZE INTO API-ENDPOINT * Make HTTP GET request CALL "HTTP-CLIENT" USING REQUEST-METHOD API-ENDPOINT REQUEST-HEADERS REQUEST-BODY RESPONSE-CODE RESPONSE-BODY IF RESPONSE-CODE = 200 DISPLAY "GET request successful" DISPLAY "Response: " RESPONSE-BODY ELSE DISPLAY "GET request failed with code: " RESPONSE-CODE END-IF. DEMONSTRATE-POST-REQUEST. DISPLAY "=== POST Request Demo ===" MOVE "POST" TO REQUEST-METHOD MOVE "application/json" TO REQUEST-HEADERS(51:16) MOVE '{"customerName":"Jane Smith","email":"jane@email.com"}' TO REQUEST-BODY * Make HTTP POST request CALL "HTTP-CLIENT" USING REQUEST-METHOD API-ENDPOINT REQUEST-HEADERS REQUEST-BODY RESPONSE-CODE RESPONSE-BODY IF RESPONSE-CODE = 201 DISPLAY "POST request successful" DISPLAY "Response: " RESPONSE-BODY ELSE DISPLAY "POST request failed with code: " RESPONSE-CODE END-IF.

Web Service Integration

Web service integration enables COBOL applications to consume and provide services using standard protocols.

SOAP Web Service Consumption

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
IDENTIFICATION DIVISION. PROGRAM-ID. SOAP-SERVICE-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 SOAP-REQUEST. 05 SOAP-ENVELOPE PIC X(2000). 05 SOAP-ACTION PIC X(100). 05 SOAP-ENDPOINT PIC X(200). 01 SOAP-RESPONSE. 05 RESPONSE-XML PIC X(3000). 05 RESPONSE-STATUS PIC 9(3). 01 CUSTOMER-SEARCH. 05 SEARCH-CRITERIA PIC X(50). 05 SEARCH-RESULTS PIC X(1000). PROCEDURE DIVISION. PERFORM BUILD-SOAP-REQUEST PERFORM CALL-SOAP-SERVICE PERFORM PROCESS-SOAP-RESPONSE STOP RUN. BUILD-SOAP-REQUEST. MOVE "http://services.example.com/CustomerService" TO SOAP-ENDPOINT MOVE "http://services.example.com/SearchCustomer" TO SOAP-ACTION MOVE '' TO SOAP-ENVELOPE(1:38) MOVE '' TO SOAP-ENVELOPE(39:75) MOVE '' TO SOAP-ENVELOPE(114:11) MOVE '' TO SOAP-ENVELOPE(125:15) MOVE 'John Doe' TO SOAP-ENVELOPE(140:26) MOVE '' TO SOAP-ENVELOPE(166:16) MOVE '' TO SOAP-ENVELOPE(182:12) MOVE '' TO SOAP-ENVELOPE(194:15). CALL-SOAP-SERVICE. CALL "SOAP-CLIENT" USING SOAP-ENDPOINT SOAP-ACTION SOAP-ENVELOPE RESPONSE-XML RESPONSE-STATUS. PROCESS-SOAP-RESPONSE. IF RESPONSE-STATUS = 200 DISPLAY "SOAP service call successful" DISPLAY "Response: " RESPONSE-XML * Parse XML response here ELSE DISPLAY "SOAP service call failed with status: " RESPONSE-STATUS END-IF.

Authentication and Security

Secure API integration requires proper authentication mechanisms and security protocols.

API Authentication Implementation

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
IDENTIFICATION DIVISION. PROGRAM-ID. API-AUTH-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 AUTHENTICATION. 05 AUTH-TYPE PIC X(10). 05 API-KEY PIC X(100). 05 ACCESS-TOKEN PIC X(200). 05 TOKEN-EXPIRY PIC 9(14). 01 SECURE-HEADERS. 05 AUTHORIZATION PIC X(200). 05 CONTENT-TYPE PIC X(50). 05 USER-AGENT PIC X(100). PROCEDURE DIVISION. PERFORM INITIALIZE-AUTHENTICATION PERFORM DEMONSTRATE-API-KEY-AUTH PERFORM DEMONSTRATE-OAUTH-AUTH STOP RUN. INITIALIZE-AUTHENTICATION. MOVE "application/json" TO CONTENT-TYPE MOVE "COBOL-API-Client/1.0" TO USER-AGENT. DEMONSTRATE-API-KEY-AUTH. DISPLAY "=== API Key Authentication ===" MOVE "API-KEY" TO AUTH-TYPE MOVE "sk-1234567890abcdef" TO API-KEY STRING "X-API-Key: " DELIMITED BY SIZE API-KEY DELIMITED BY SIZE INTO AUTHORIZATION DISPLAY "Using API Key authentication" DISPLAY "Authorization header: " AUTHORIZATION. DEMONSTRATE-OAUTH-AUTH. DISPLAY "=== OAuth Authentication ===" MOVE "OAUTH" TO AUTH-TYPE MOVE "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." TO ACCESS-TOKEN STRING "Authorization: Bearer " DELIMITED BY SIZE ACCESS-TOKEN DELIMITED BY SIZE INTO AUTHORIZATION DISPLAY "Using OAuth Bearer token" DISPLAY "Authorization header: " AUTHORIZATION.

Error Handling and Monitoring

Robust API integration requires comprehensive error handling and monitoring capabilities.

API Error Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
IDENTIFICATION DIVISION. PROGRAM-ID. API-ERROR-HANDLING. DATA DIVISION. WORKING-STORAGE SECTION. 01 API-RESPONSE. 05 RESPONSE-CODE PIC 9(3). 05 RESPONSE-MESSAGE PIC X(200). 05 ERROR-DETAILS PIC X(500). 01 RETRY-CONTROL. 05 RETRY-COUNT PIC 9(2) VALUE ZERO. 05 MAX-RETRIES PIC 9(2) VALUE 3. 05 RETRY-DELAY PIC 9(3) VALUE 1000. PROCEDURE DIVISION. PERFORM CALL-API-WITH-RETRY STOP RUN. CALL-API-WITH-RETRY. PERFORM UNTIL RETRY-COUNT >= MAX-RETRIES PERFORM MAKE-API-CALL IF RESPONSE-CODE = 200 OR RESPONSE-CODE = 201 DISPLAY "API call successful" EXIT PERFORM ELSE ADD 1 TO RETRY-COUNT PERFORM HANDLE-API-ERROR IF RETRY-COUNT < MAX-RETRIES PERFORM WAIT-BEFORE-RETRY END-IF END-IF END-PERFORM. MAKE-API-CALL. * Simulate API call MOVE 500 TO RESPONSE-CODE MOVE "Internal Server Error" TO RESPONSE-MESSAGE. HANDLE-API-ERROR. DISPLAY "API Error - Attempt " RETRY-COUNT " of " MAX-RETRIES DISPLAY "Response Code: " RESPONSE-CODE DISPLAY "Error Message: " RESPONSE-MESSAGE PERFORM LOG-API-ERROR. LOG-API-ERROR. DISPLAY "Logging API error for monitoring" CONTINUE. WAIT-BEFORE-RETRY. DISPLAY "Waiting " RETRY-DELAY "ms before retry..." CONTINUE.

Best Practices for API Integration

Following best practices ensures reliable, secure, and maintainable API integration in COBOL applications.

Design Principles

  • Use appropriate HTTP methods for different operations
  • Implement proper error handling and retry mechanisms
  • Secure all API communications with proper authentication
  • Validate all input and output data
  • Implement comprehensive logging and monitoring

Performance Considerations

  • Use connection pooling for high-volume applications
  • Implement caching for frequently accessed data
  • Optimize JSON/XML processing for large payloads
  • Consider asynchronous processing for long-running operations
  • Monitor API response times and throughput

FAQ

How do you integrate COBOL with REST APIs?

COBOL REST API integration involves using HTTP client libraries, JSON parsing, and web service calls. Modern COBOL compilers provide built-in support for HTTP operations and JSON processing, enabling seamless integration with RESTful services.

What is JSON processing in COBOL?

JSON processing in COBOL involves parsing JSON data structures, extracting values, and creating JSON responses. COBOL provides intrinsic functions like JSON PARSE and JSON GENERATE for handling JSON data in modern applications.

How do you make HTTP requests from COBOL?

HTTP requests from COBOL are made using web service calls, HTTP client libraries, or built-in HTTP functions. This includes GET, POST, PUT, and DELETE operations with proper headers and authentication.

What are web services in COBOL?

Web services in COBOL enable communication with external systems using standard protocols like HTTP/HTTPS, SOAP, and REST. They allow COBOL applications to consume and provide services over networks.

How do you handle authentication in COBOL API integration?

Authentication in COBOL API integration includes API keys, OAuth tokens, basic authentication, and certificate-based authentication. Proper security headers and token management are essential for secure API communication.