How to consume JSON RO Web services?

Hi,

refer to the document http://wiki.remobjects.com/wiki/WIP:How_to_consume_RemObjects_SOAP_services_with_third_party_clients

i manage to consume the soap services, would like to check how about the JSON ? i have add in the JSON message type in to server channel. Below is my test in client program:

protected void btnSoap_Click(object sender, EventArgs e)
{
System.ServiceModel.Channels.Binding b = (System.ServiceModel.BasicHttpBinding)Activator.CreateInstance(typeof(System.ServiceModel.BasicHttpBinding));
System.ServiceModel.EndpointAddress eAddress = new System.ServiceModel.EndpointAddress(“http://localhost:8099/soap”);

        soapServices.ROServer1ServiceClient iTest = new soapServices.ROServer1ServiceClient(b, eAddress);

        var msgHeader = new soapServices.ROClientIDHeader();
        msgHeader.ID = String.Concat("{", Guid.NewGuid().ToString(), "}");

        var result = iTest.GetServerTime(ref msgHeader);
        txtResultSOAP.Text = result.ToString();
    }

Thanks.

You’d need to compose a requiest JSON string, send it to the server at http://[servername]:[serverport]/json (using .NET WebClient or similar class), then get back a response JSON string and parse it.

http://json2csharp.com/ can help you to generate .NET classes that match the provided JSON string.
Requested JSON string formats can be viewed using Fiddler: create RO SDK client and server and inspect the traffic between them using this awesome tool.

1 Like

Hi, we use the sample server, JSON message, when i consume the services,

http://localhost:8099/json/sum?a=11&b=22

should expect below result

{“version”:“1.1”,“result”:“33”}

but it return error as below:

Server Error

The following error occurred:

500 Invalid path specified: ‘/json/sum?a=1&b=2’.

at RemObjects.SDK.Server.IpHttpServerChannel.HandleHttpRequest(Object sender, HttpRequestEventArgs e)
Framework Version v4.0.30319.34209
RemObjects SDK for .NET v8.3.91.1167
Internet Pack for .NET v8.3.91.1167

RemObjects Software, LLC. remobjects.com.

Requests where parameters are passed via URL are not supported. You need to pass the request JSON string via request body, not its Url

Hi, i have done some research and most of the way is using URL. Anywhere, i have try to code as below to get the JSON data back, but the server return error. Or might be can you give some code to show how it work ?

    protected void btnServerDateTimeJSON_Click(object sender, EventArgs e)
    {
        string urlPath = "http://localhost:8099/json/SUM";

        string data = "A=11&B=22"; //replace <value>
        byte[] dataStream = Encoding.UTF8.GetBytes(data);
        
        WebRequest webRequest = WebRequest.Create(urlPath);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = dataStream.Length;  
        Stream newStream = webRequest.GetRequestStream();
        // Send the data.
        newStream.Write(dataStream, 0, dataStream.Length);
        newStream.Close();
       
        using (var httpWebResponse = webRequest.GetResponse() as HttpWebResponse)
        {
            if (httpWebResponse != null)
            {
                using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    txtResultJSON.Text = streamReader.ReadToEnd();
                }
            }
        }
    }

Server expexts a JSON string in the request body. Like f.e.

{"version":"1.1","method":"ROServer2Service.Sum","params":{"A":3,"B":4}}

Then response will be, f.e.

{"version":"1.1","result":7}

the correct JSON response from server is

{“version”:“1.1”,“result”:{“Result”:33},“id”:"{fe1fe652-bc39-4f25-b66a-b5511c3150e7}"}

for those interested on .net client to read it, you can refer to below code:

c# .net
var jsonResult = new JavaScriptSerializer().Deserialize(result);
txtResultJSONGetSum.Text = (jsonResult[“result”][“Result”]).ToString();

Thanks.

This is the JSON message version. Assume it always be 1.1

This is the remote session Id. Can be useful for authorization tasks.

Data payload (server method result).

One can use tools like http://json2csharp.com/ to convert JSON strings into corresponding C# data structures.

Any way to get that working as a standard json call?

http://localhost:8099/json/ROServer2Service/Sum/3/4

Best regards

Yes. You need to use HttpAPI: https://talk.remobjects.com/t/new-ro-sdk-feature-http-api/12993

another way:
you can transform this string into valid json request in HTTPServer.OnCustomResponseEvent event.

see more at Using OnCustomResponseEvent in a ROSDK Server snippet

1 Like