Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get response content #136

Closed
inikonorov opened this issue Apr 16, 2019 · 8 comments
Closed

get response content #136

inikonorov opened this issue Apr 16, 2019 · 8 comments
Labels

Comments

@inikonorov
Copy link

How can I get content of response in njs?

@xeioex
Copy link
Contributor

xeioex commented Apr 16, 2019

@inikonorov

See the reference

r.responseBody
holds the subrequest response body, read-only. The size of r.responseBody is limited by the subrequest_output_buffer_size directive.

Please note that responseBody is available only for response request in a subrequest callback.

function content(r) {
     function callback(response) {
         // response.responseBody is available here;
     }

    r.subrequest('/foo', callback);
}

@inikonorov
Copy link
Author

inikonorov commented Apr 16, 2019

but this method works only for subrequest. but what should I do if I don't need to get response of subrequest? for example: I go on site and I want to get the response of this request (i.e. html code)

@drsm
Copy link
Contributor

drsm commented Apr 16, 2019

@inikonorov
there is no general purpose HTTP client yet.
but maybe #101 would help you as an example.

@inikonorov
Copy link
Author

Thank you very much!

@maniLonkar
Copy link

maniLonkar commented Jun 2, 2019

@drsm @inikonorov How did you get the response body exactly? I am trying to log the response body in a file. This is my js file

function logger(r) {
    var time = r.variables.time_local;
    var method = r.method;
    var uri = r.uri;
    var status = r.status;
    var reqBody = r.requestBody || {};
    var args = r.args == undefined || r.args == null ? {} : r.args;
    var session = getSession(r.headersIn);
    var log = "";
    r.subrequest(uri, function(res) {
        r.log("respBody");
        r.log(res.responseBody);
        return JSON.stringify({
            "time": time,
            "method": method,
            "uri": uri,
            "status": status,
            "req_body": r.requestBody,
            "session": session,
            "resp_body": "res.responseBody",
        });
    });
}

I tried above but getting error 1 pending events while closing request

@drsm
Copy link
Contributor

drsm commented Jun 3, 2019

@maniLonkar
Hi!

Subrequests are asynchronous and you should end the main request manually when all or some of them are complete. The return value of r.subrequest makes no sense, as there nowhere to return.

function logger(r) {
    var time = r.variables.time_local;
    var method = r.method;
    var uri = r.uri;
    var status = r.status;
    var reqBody = r.requestBody || {};
    var args = r.args == undefined || r.args == null ? {} : r.args;
    var session = getSession(r.headersIn);
    var log = "";
    r.subrequest(uri, function(res) {
        r.log("respBody");
        r.log(res.responseBody);
        r.return(200, JSON.stringify({
            "time": time,
            "method": method,
            "uri": uri,
            "status": status,
            "req_body": r.requestBody,
            "session": session,
            "resp_body": "res.responseBody",
        });
    }));
}

@dmitry-j-mikhin
Copy link

I've managed to get full response body content using js_body_filter, that later can be used for logging, example is here - https://github.com/dmitry-j-mikhin/nginx-njs-log

@kalungedamaji
Copy link

kalungedamaji commented May 7, 2024

@dmitry-j-mikhin It worked thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants