large request body in njs

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

large request body in njs

Jonathan Esterhazy
Hello!

I am trying to use njs (ngx_http_js_module) to modify POSTed request data before sending to an upstream api. Using the req.requestBody function works fine for small requests, but for larger ones causes this error:

[error] 14#14: *18 js exception: Error: request body is in a file

If I was using the Lua module, I could use ngx.req.get-body_file function to get this data, but there doesn't seem to be any way to do that in njs. Did I miss something? Is there a way to access the data or find out the filename?


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: large request body in njs

Dmitry Volyntsev


On 29 Oct 2018, at 23:44, Jonathan Esterhazy <[hidden email]> wrote:

Hello!

I am trying to use njs (ngx_http_js_module) to modify POSTed request data before sending to an upstream api. Using the req.requestBody function works fine for small requests, but for larger ones causes this error:

[error] 14#14: *18 js exception: Error: request body is in a file

If I was using the Lua module, I could use ngx.req.get-body_file function to get this data, but there doesn't seem to be any way to do that in njs. Did I miss something? Is there a way to access the data or find out the filename?


Hi Jonathan!

You have two options here:

1) you can increase the client buffers

According to the documentation:
r.requestBody
returns the client request body if it has not been written to a temporary file. To ensure that the client request body is in memory, its size should be limited by client_max_body_size, and a sufficient buffer size should be set using client_body_buffer_size.

2) you can open the file with the client’s request using request_body_file variable (http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_body_file)

var fs = require(‘fs’);
var large_body = fs.readFileSync(r.variables.request_body_file)



_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: large request body in njs

Jonathan Esterhazy
Yes, these suggestions worked. Thanks!
On Oct 30, 2018, 2:14 AM -0700, Dmitry Volyntsev <[hidden email]>, wrote:


On 29 Oct 2018, at 23:44, Jonathan Esterhazy <[hidden email]> wrote:

Hello!

I am trying to use njs (ngx_http_js_module) to modify POSTed request data before sending to an upstream api. Using the req.requestBody function works fine for small requests, but for larger ones causes this error:

[error] 14#14: *18 js exception: Error: request body is in a file

If I was using the Lua module, I could use ngx.req.get-body_file function to get this data, but there doesn't seem to be any way to do that in njs. Did I miss something? Is there a way to access the data or find out the filename?


Hi Jonathan!

You have two options here:

1) you can increase the client buffers

According to the documentation:
r.requestBody
returns the client request body if it has not been written to a temporary file. To ensure that the client request body is in memory, its size should be limited by client_max_body_size, and a sufficient buffer size should be set using client_body_buffer_size.

2) you can open the file with the client’s request using request_body_file variable (http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_body_file)

var fs = require(‘fs’);
var large_body = fs.readFileSync(r.variables.request_body_file)



_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx