# HG changeset patch # User Yaroslav Zhuravlev # Date 1612442952 0 # Node ID 9fc25ea7a92cf3d33a33bf4d70435c5e8df54f7b # Parent eb508d8c1c31d7bafceed2bf62841f2ec606e6c1 Documented ngx.fetch and Response interface in njs. diff --git a/xml/en/docs/njs/reference.xml b/xml/en/docs/njs/reference.xml --- a/xml/en/docs/njs/reference.xml +++ b/xml/en/docs/njs/reference.xml @@ -9,7 +9,7 @@
+ rev="61">
@@ -580,6 +580,105 @@ on the warning level
+
+ + +The Response interface is available since +0.5.1. + + + +arrayBuffer() + +Takes a Response stream and reads it to completion. +Returns a Promise that resolves with +an ArrayBuffer. + + +bodyUsed + +A boolean value, true +if the body was read. + + +headers + +The Headers read-only object associated with the +Response: + + + +get(name) + +returns a string containing the values of all headers with the specified name +separated by a comma and a space + + +getAll(name) + +returns an array containing the values of all headers with the specified name + + +has(name) + +returns a boolean value +indicating whether a header with the specified name exists + + + + + +json() + +Takes a Response stream and reads it to completion. +Returns a Promise that resolves with +the result of parsing the body text as JSON. + + +ok + +A boolean value, true +if the response was successful (status codes between 200–299). + + +redirected + +A boolean value, true +if the response is the result of a redirect. + + +status + +The status code of the response. + + +statusText + +The status message corresponding to the status code. + + +text() + +Takes a Response stream and reads it to completion. +Returns a Promise that resolves with a string. + + +type + +The type of the response. + + +url + +The URL of the response. + + + + + +
+ +
@@ -587,6 +686,60 @@ The ngx global object since 0.5.0. +ngx.fetch(url, +[options]) + +Makes a request to fetch an URL +(0.5.1), +returns a Promise that resolves with +the Response object. +Only the http:// scheme is supported, +redirects are not handled. + +The options parameter is expected to be an object +with the following keys: + + +body + +request body, +by default is empty + + +buffer_size + +the buffer size for reading the response, +by default is 4096 + + +headers + +request headers object + + +max_response_body_size + +the maximum size of the response body in bytes, +by default is 32768 + + +method + +HTTP method, +by default the GET method is used + + + +Example: + +ngx.fetch('http://nginx.org/') +.then(reply => reply.text()) +.then(body => r.return(200, body)) +.catch(e => r.return(501, e.message)) + + + + ngx.log(level, message) @@ -598,6 +751,7 @@ The following log levels can be specifie ngx.WARN, and ngx.ERR. +