Comments (8)
Hi @Masterwow3,
I cannot reproduce the problem so far, I am using the following config and js code
error_log /dev/stdout info;
daemon off;
master_process off;
events { }
http {
js_import main from fetch.js;
resolver 1.1.1.1;
server {
listen 8000;
location / {
js_content main.fetch;
}
}
server {
listen 8001;
location / {
js_content main.reply;
}
}
}
async function fetch(r) {
let reply = await ngx.fetch("http://127.0.0.1:8001/",
{ headers: { 'x-ms': "a", 'foo': 'xxx' } })
let body = await reply.text();
r.return(200, body);
}
function reply(r) {
let body = [njs.version, '\n'];
r.rawHeadersIn.forEach((header) => {
body.push(`${header[0]}: ${header[1]}\n`);
});
r.return(200, body.join(''));
}
export default { fetch, reply };
To run the nginx:
nginx -c fetch.conf
curl http://127.1:8000/
0.7.10
Host: 127.0.0.1
Connection: close
x-ms: a
foo: xxx
I tried njs versions from 0.7.10 to 0.7.12 and the latest one 0.8.2. Which njs version are you using?
from njs.
Hi @xeioex,
thanks for your reply, you are right, the mirror I was using swallowed the headers.
But there is still a problem. The header Content-Length
cannot be set. This does not reach the server.
Log output: [info] 29#29: *3 js: [["Host","10.3.2.52"],["Connection","close"],["x-ms","123"]]
nginx/1.24.0
async function send(r) {
await ngx.fetch("http://10.3.2.52:8082/", {
headers: {
"Content-Length": "0",
"x-ms": "123"
}
});
r.return(200);
}
async function log(r) {
r.log(JSON.stringify(r.rawHeadersIn));
r.return(200);
}
export default { send, log }
server {
listen 8081;
js_import njs/test.js;
location / {
js_content test.send;
}
}
server {
listen 8082;
js_import njs/test.js;
location / {
js_content test.log;
}
}
from njs.
Hi @Masterwow3,
"Content-Length" and a handful of other headers are treated in a special way by nginx. For example, with "Content-Length", the value is put to r->headers_in.content_length
as a number and not as a header.
Whereas both r.rawHeadersIn
and r.headersIn
look for ordinary headers.
In njs
you can get "Content-length" by r.requestBuffer.length
for the text input.
from njs.
Hi @xeioex,
curl -v -H "Content-Length: 0" "http://10.3.2.52:8082/"
Arrives at nginx: [info] 29#29: *2 js: [["Host","10.3.2.52:8082"],["User-Agent","curl/7.87.0"],["Accept","*/*"],["Content-Length","0"]]
It does not arrive per NJS.
await ngx.fetch("http://10.3.2.52:8082/", {
headers: {
"Content-Length": "0",
"x-ms": "123"
}
});
Does that mean I can't send this header via njs?
from njs.
Hi @Masterwow3,
There are no special treatment of Content-Length
in njs Fetch for headers object.
With this code https://gist.github.com/xeioex/f3ceec948833c254dfb51bee4b7e48eb
I get the following output:
curl http://127.1:8000/
0.8.3
Host: 127.0.0.1
x-ms: a
foo: xxx
Content-Length: 0
Connection: close
Can you confirm the njs version? Just output njs.version in JS code.
from njs.
Hi @xeioex,
[info] 28#28: *1 js: NJS version: 0.7.12
Okay too bad, is it planned to support the header Content-Length
in NJS?
I use the Microsoft Blob API which requires this header.
from njs.
Hi @Masterwow3,
There WAS a special treatment for Content-Length and other headers in accordance with Fetch API (between 0.7.10 and 0.7.12). But this was reverted back since 0.8.0. The latest released version is 0.8.2.
Feel free to upgrade to nginx 1.25.3 and njs 0.8.2.
from njs.
nginx/1.25.3
NJS version: 0.8.2
28#28: *3 js: [["Host","10.3.2.52"],["Content-Length","0"],["x-ms","123"],["Connection","close"]]
Thank you, it works.
from njs.
Related Issues (20)
- context for js_var variables HOT 2
- First chunk detection in js_body_filter HOT 1
- how to hide response header : server HOT 3
- How to log to a separate log file instead of error log file? HOT 2
- Detached r.subrequest not running HOT 4
- ngx.shared.SharedDict.clear() fails! HOT 2
- Shared library and installation
- js_periodic / Reading status connections growing HOT 3
- How to implement an external rate limitter? HOT 2
- njs_string_atob cannot handle cypertext unless the padding is intact HOT 1
- Add Support for js_balancer like balancer_by_lua_block HOT 2
- How to remove or override value of 'Server' header for all locations. HOT 10
- Introducing QuickJS engine as alternative JS engine. HOT 7
- ngx.shared.SharedDict.clear() hangs when called on an empty dict HOT 1
- NJS call wrong function. HOT 7
- Sending a Buffer with r.send() does not work HOT 1
- Repo looks empty
- Test failure with zlib-ng HOT 6
- False Positive Duplicate Checker Warning HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from njs.