简体   繁体   中英

How to pass non xml data in header in gsoap C++ from client?

I am using gsoap c++ library to make a java web service call.

I can pass a json file by calling json_call, but i want to add header information. For that i modified the struct SOAP_ENV__Header as:

struct ns3__Header   {       
  char *username;          
  char* password;   };   
struct SOAP_ENV__Header    {
  #ifdef WITH_NOEMPTYSTRUCT
  char dummy;    
  #endif
  struct ns3__Header *ns3__MyHeader;
};

Now i can add values in header, but my problem is they are getting added in xml format to the header. But I want just tag:value format. How to achieve that?

When I pass a header, it looks like:

<?xml version="1.0" encoding="UTF-8"?>   <username id="_300">        <ns3:MyHeader> 
        in soap_out_SOAP_ENV__Header
    </ns3:MyHeader>   </username>   {     "add":      {       "i": 10,      "j": 20,  
 }   }

But it should look like:

username:xyz
password:abcd
...
...
{
  "add": 
  {
    "i": 10,
    "j": 20,

  }
} 

Soap_Env_header通过xml发送默认值,因此无法在json中推送它,请尝试其他库希望是cpprest(Casablanca)

Finally i found a solution to pass header info in rest call in c++ gsoap -

#include "json.h"
#include <string.h>
#include "jsonStub.h"

struct Namespace namespaces[] = { {NULL, NULL} };
int main()
{
   struct soap *ctx = soap_new1(SOAP_XML_NOTYPE);
   soap_init(ctx);
   struct value *request = new_value(ctx);
   struct value response;

   ctx->sendfd = 1;   
   ctx->http_extra_header = "userName:abcd\r\npassword:xyz";
   *string_of(value_at(value_at(request, "add"), "i")) = "10";
   *string_of(value_at(value_at(request, "add"), "j")) = "20";

   json_write(ctx, request);
   printf("\n");
   if (json_call(ctx, "endpoint",request, &response))
   {
         printf( "json call failed " );
         soap_print_fault(ctx, stderr);
         printf("\n%d", ctx->error);
         printf("\n1: SOAP faultcode = %s\n", *soap_faultcode(ctx));
         printf("2: SOAP faultstring = %s\n", *soap_faultstring(ctx));
         printf("3: SOAP faultdetail = %s\n\n", *soap_faultdetail(ctx));
         soap_print_fault_location(ctx, stderr);

   }

   else
   {
         printf("Success !!!");
         json_write(ctx, &response);
   }

   soap_destroy(ctx);
   soap_end(ctx);
   soap_free(ctx);
   return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM