NGINX status code based on *response* body

Muhammad Nuzaihan zaihan at unrealasia.net
Sun Sep 24 16:01:11 UTC 2023


Hi,

I would like to return an error message based on the *response* body.

I know i can do it easily with *request* body (with "return
NGX_HTTP_BAD_REQUEST") but my case i want to do it with *response*
body.

Also i have tried this code with response body filter but i got "*1
header already sent, client: 127.0.0.1, server: localhost":

Thank you for your guidance.

Code:

```
    return ngx_http_filter_finalize_request(request,
                                         &ngx_m_module,
                                          NGX_HTTP_BAD_REQUEST);
```
RESPONSE BODY CODE:

ngx_int_t ResponseBodyFilter(ngx_http_request_t *request,
                                     ngx_chain_t *chain_head) {
  // Set the logging level to debug
  // TODO: remove
  request->connection->log->log_level = NGX_LOG_DEBUG;

  // Get our context so we can store the response body data
  FilterContext *ctx = GetModuleFilterContext(request);
  if (ctx == NULL) {
    return NGX_ERROR;
  }

    return ngx_http_filter_finalize_request(request,
                                          &ngx_my_module,
                                          NGX_HTTP_BAD_REQUEST);

}

ngx_int_t ModuleInit(ngx_conf_t *cf) {
  kNextRequestBodyFilter = ngx_http_top_request_body_filter;
  ngx_http_top_request_body_filter = RequestBodyFilter;

  kNextResponseBodyFilter = ngx_http_top_body_filter;
  ngx_http_top_body_filter = ResponseBodyFilter;

  kNextHeaderFilter = ngx_http_top_header_filter;
  ngx_http_top_header_filter = HeaderFilter;

  return NGX_OK;
}


More information about the nginx-devel mailing list