Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
Highlighted
marcia Absent Member.
Absent Member.
300 views

Visual Cobol + Post + Json

Hello!

I would like to know how to use webservice (post + json) in Visual Cobol. Is this possible in VisualCobol?

Below are two examples in C # that I tried to translate, but it did not work.

Can someone help me?

 

 

First example:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
user = "Foo",
password = "Baz"
});

streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}

 

Another example:

 

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"user\":\"test\"," +
"\"password\":\"bla\"}";

streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}

 

I'm waiting. Thanks.

Marcia Pires
marcia.pires@mmsis.com.br


0 Likes
3 Replies
Micro Focus Frequent Contributor
Micro Focus Frequent Contributor

RE: Visual Cobol + Post + Json

You haven't told us what version of Visual COBOL you're using. Please include product, version, and platform with each question. In this case we know you're using Visual COBOL, and I'll assume you're writing managed COBOL for .NET, since you refer to C# examples. I'll assume you're on the latest version.

You also haven't shown us your attempts to translate the C# into managed COBOL, which might be a better starting point. That said, here's a COBOL version (off the top of my head) for the first example:

declare httpWebRequest = WebRequest::Create("http://url") as type HttpWebRequest
set httpWebRequest::ContentType to "application/json"
set httpWebRequest::Method to "POST"

perform using streamWriter = new StreamWriter(httpWebRequest::GetRequestStream)
declare jsSerializer = new JavasScriptSerializer
declare json as string = jsSerializer::Serialize(new MyJson(user = "Foo", password = "Baz"))
streamWriter::Write(json);
end-perform

declare httpResponse = httpWebRequest::GetResponse as type HttpWebResponse
declare result as string
perform using streamReader = new StreamReader(httpResponse::GetResponseStream)
set result to streamReader::ReadToEnd
end-perform

...

class-id MyJson.
1 user public string property as "user".
1 password public string property as "password".
end class MyJson.

Untested, and may require some changes to compile and work correctly. You'll need all the appropriate references and namespaces specified in the project, or (in the case of namespaces) with $set directives.

The biggest difference here is that, as far as I know, managed COBOL does not have syntax support yet for anonymous objects, which the C# example uses when it specifies the object to be serialized to JSON. Consequently I had to define a class (MyJson) to represent that object. That class definition can be in a separate COBOL source file in the project, in the same COBOL source file as the web-service-client code as another top-level class, or within the top-level class (particularly if you don't need that definition elsewhere).

greiner Absent Member.
Absent Member.

RE: Visual Cobol + Post + Json

i have Need an good converter for json-files and have written in Cobol my own program. I know that with dotnet there are better Tools if you have the Schema Explanation. And when not? The same Problem...
0 Likes
Micro Focus Frequent Contributor
Micro Focus Frequent Contributor

RE: Visual Cobol + Post + Json

If you don't have an object type corresponding to an incoming JSON message, you can use JavaScriptSerializer::DeserializeObject to deserialize it to a generic object. Then you can use reflection to query the attributes of that object.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.