loentar / ngrest

Fast and easy C++ RESTful WebServices framework
Apache License 2.0
465 stars 94 forks source link

setting content-type for messages handled using Filter, bypassing usual processing by ngrest engine #80

Closed raadiy closed 2 years ago

raadiy commented 2 years ago

Hi Dmitry, Is there a method or function to set the content-type for messages handled using filters (the one i was referring in the other issue today). Right now, inside the service, the response seems to go out as text content-type. Is there a way to set the content-type to json?. Thanks in advance.

==== code snippet ====

      if (headerBody != nullptr) {
          const char* body = headerBody->value;
          req += body;
          uint64_t bodySize = context.request->bodySize;
          printf("\n patch req value <%s>\n", body);
          my_msghandler_function( (void *) req.c_str(), resp);
          context.response->poolBody->putData(resp, strlen(resp)); // Before this, is there a way to set the content-type to json
      }

====

Regards Raajesh

loentar commented 2 years ago

If you mean to set up Content-Type of response:

    HttpResponse* response = static_cast<HttpResponse*>(context.response);
    response->headers = context.pool->alloc<Header>("Content-Type", "application/json", response->headers);

https://github.com/loentar/ngrest/blob/master/services/favicon/src/Favicon.cpp#L36

raadiy commented 2 years ago

It works. Thank you again. Regards Raajesh