Welcome! Log In Create A New Profile

Advanced

NGINX status code based on *response* body

Muhammad Nuzaihan
September 24, 2023 12:02PM
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;
}
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

NGINX status code based on *response* body

Muhammad Nuzaihan 237 September 24, 2023 12:02PM

Re: NGINX status code based on *response* body

Roman Arutyunyan 84 September 25, 2023 10:56AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 286
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready