How to make http requests from COBOL?
For example, how can I create GET
, POST
requests to http://example.com
?
Relevant information about what I have on my machine:
$ cobc --version
cobc (OpenCOBOL) 1.1.0
Copyright (C) 2001-2009 Keisuke Nishida / Roger While
Built Nov 02 2013 00:16:01
Packaged Feb 06 2009 10:30:55 CET
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.1 LTS"
Currently, we are connecting getting information from web service through cobol by using "db2 xml" "SOAP HTTP NV". exec sql select db2xml.soaphttpnv (:internet protocol address,:webservice namespace,:soap xml schema structure) into :varchar-output from sysibm.sysdummy1 end-exec.
HTTP messages are how data is exchanged between a server and a client. In this, there are two types of messages where one is HTTP client request and the second is the response from the server. Messages in textual form and it is encoded in ASCII form, and span over multiple lines.
HTTP’s communication happens using this established TCP connection (see the bottom part that lists outs the protocols used for the resource transfer) The client sends the HTTP Request line to the server (Line 6) to which the server responds with the HTTP Status code & data as we saw earlier in the telnet output
More than twenty years after HttpURLConnection we had Black Panther in the cinemas and a new HTTP client added to Java 11: java.net.http.HttpClient. This has a much more logical API and can handle HTTP/2, and Websockets. It also has the option to make requests synchronously or asynchronously by using the CompletableFuture API.
Steve Williams, as part of his work with tutorial level, but useful, COBOL sources, has posted some samples highlighting socket programming with GnuCOBOL.
Too long to list here, but the RESTClient sample is well worth reading, and learning from.
http://sourceforge.net/p/gnucobol/discussion/help/thread/762285b2/7ada/attachment/RESTclient.tar.gz
That's not the only way to go about http request/response handling in COBOL, but it's working code and a handy leg up.
See http://sourceforge.net/p/gnucobol/discussion/contrib/thread/2b474086 for the related conversation on SourceForge.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With