nginScript send POST resquest

Roman Arutyunyan arut at nginx.com
Mon Feb 26 11:53:46 UTC 2018


Hi,

On Sat, Feb 24, 2018 at 05:37:45AM -0500, matke wrote:
> Hi,
> I tried to send some POST request to remote server (as part of external
> authentication),  but I got message that XMLHttpRequest is undefined (or
> similar error). 
> I saw later in this post
> https://forum.nginx.org/read.php?2,275459,275459#msg-275459 answer that this
> is not possible.

Exactly.  It's not possible.

This page describes current state of njs:

http://nginx.org/en/docs/njs_about.html

> Can it help if NodeJS is installed, or nginScript it completely independent?
> (I don't have much experience in any javascript stuff)

NodeJS and nginScript are completely independent.

We are currently working on a solution, which allows making HTTP requests
from njs.

> This is how my script looks:
> 
> 
> [code]
> function baz(req, res) {
>     res.headers.foo = 1234;
>     res.status = 200;
>     res.contentType = "text/plain; charset=utf-8";
>     res.contentLength = 22;
>     res.sendHeader();
>     res.send("Enginx ");
>     res.send("java");
>     res.send("script\r\n");
>     var response = "resp";
> 
> 
>     var data = "{\n  \"page\": 0,\n  \"count\": 1,\n  \"order\": 2,\n 
> \"sort\": \"0\",\n  \"headers\": {\n    \"Access-Control-Allow-Origin\": [\n
>      \"*\"\n    ]\n  }\n}";
> 
>     var xhr = new XMLHttpRequest();
>     xhr.withCredentials = true;
> 
>     xhr.addEventListener("readystatechange", function () {
>       if (this.readyState === 4) {
>     //    console.log(this.responseText);
>       }
>     });
> 
>     xhr.open("POST", "https://remote.host/api/login", false);
> 
>     xhr.setRequestHeader("origin", "https://localhost");
>     xhr.setRequestHeader("content-type", "application/json");
>     xhr.setRequestHeader("referer", "https://localhost/");
>     xhr.setRequestHeader("accept-encoding", "gzip, deflate, br");
>     xhr.setRequestHeader("accept-language",
> "sr-RS,sr;q=0.9,en-US;q=0.8,en;q=0.7,hr;q=0.6,bs;q=0.5");
>     xhr.setRequestHeader("cookie",
> "securitytoken=eyJraWQiOiJ.......Swnq3xjEvXodQ");
> 
>     xhr.send(data);
> 
>     xhr.onreadystatechange = processRequest;
> 
>     function processRequest(e) {
>         if (xhr.readyState == 4 && xhr.status == 200) {
>             response = JSON.parse(xhr.responseText);
>            // alert(response.ip);
>         }
>     }
> 
>     res.send(response);
>     res.finish();
> }
> [/code]
> 
> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,278746,278746#msg-278746
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx

-- 
Roman Arutyunyan


More information about the nginx mailing list