Search code examples
c++soapgsoap

how to add tag in soap response


Using gsoap I have generated server C++ codes.

Using SoapUI, I send a message to server and get the response. Until here every thing is fine.

I wanted to add more tags on the response. To do that, I have manipulated server codes. Originally the code generated by gsoap that produces the response is:

if (soap_end_count(soap)
            || soap_response(soap, SOAP_OK)
            || soap_envelope_begin_out(soap)
            || soap_putheader(soap)
            || soap_body_begin_out(soap)
            || tempuri__IsAliveResponse.soap_put(soap, "tempuri:IsAliveResponse", "")
            || soap_body_end_out(soap)
            || soap_envelope_end_out(soap)
            || soap_end_send(soap))
        return soap->error;

This gives me the response below:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ser="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:tempuri="http://tempuri.org/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <tempuri:IsAliveResponse/>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

But when I want to add some tag more in the response:

if (soap_end_count(soap)
            || soap_response(soap, SOAP_OK)
            || soap_envelope_begin_out(soap)
            || soap_putheader(soap)
            || soap_body_begin_out(soap)
            || tempuri__IsAliveResponse.soap_put(soap, "tempuri:IsAliveResponse", "")
            || tempuri__IsAliveResponse.soap_put(soap, "newTag", "")
            || soap_body_end_out(soap)
            || soap_envelope_end_out(soap)
            || soap_end_send(soap))
        return soap->error;

Then I get a cut, partial response:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ser="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:tempuri="http://tempuri.org/">
    <SOAP-ENV:Header></SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <tempuri:IsAliveResponse></tempuri:IsAliveResponse>
        <newTag></newTag>
    </SOAP-ENV:Body>
</

Then I have discovered that Content-Length: is always 524. Moreover, every character that I add is counted as 2.

I do not see where is decided this fixed 524 length. Does it come from a buffer size? If so, how could I generate the code with larger buffer size?

How could I add more data on response in a gsoap generated C++ soap server code?


Solution

  • To add more parameter tags to the response is easy. Just edit the interface .h file (generated by wsdl2h). This file has a tempuri__IsAliveResponse class. Add more members to this class to add the response parameters you want. Then let soapcpp2 generate fresh source code with the additional params. These tags will appear within the IsAliveResponse tag.

    To create a response with multiple tags that aren't nested in IsAliveResponse, define a class with two leading underscores to make that class "invisible" to the serializer, for example class __tempuri__MyResponse. Then add the member tempuri__IsAliveResponse tempuri__IsAliveResponse_ (note the ending _ here to avoid a name clash) and other members that you want as tags. Use the new class __tempuri__MyResponse as the response type for the service operation.