tarantool / nginx_upstream_module

Tarantool NginX upstream module (REST, JSON API, websockets, load balancing)
Other
174 stars 18 forks source link

Tarantool nginx result does not display in browser #125

Closed jobs-git closed 5 years ago

jobs-git commented 5 years ago

I used the REST example from the readme however, instead of displaying in browser, the result is displayed as json. How to fix this?

Totktonada commented 5 years ago

Set default_type application/json; inside a location directive.

jobs-git commented 5 years ago

It now displays in browser however I am still getting json as output, I tried the sample here https://github.com/tarantool/nginx_upstream_module/#http-headers-and-status and configured ngx_lua but I still get json when I access the tnt_proxy

this is the output I am getting

{"id":0,"result":[200,{"content-type":"text/html; charset=utf8"},"It works!"]}

Totktonada commented 5 years ago

But in the example we extract 'result' field and use third value from it ("It works!" in your case) as the body. The example encodes the result into json before sending (so you'll get "It works!" in double quotes), it can be overcomed like so:

Try to replace the following lines:

           ngx.status = result[1]
           for k, v in pairs(result[2]) do
             ngx.header[k] = v
           end

           table.remove(result, 1)
           ngx.say(cjson.encode(result))

With the following:

                ngx.status = result[1]
                for k, v in pairs(result[2]) do
                    ngx.header[k] = v 
                end 

                local body = result[3]
                if type(body) == "string" then
                    ngx.header["content_type"] = "text/plain"
                    ngx.print(body)
                elseif type(body) == "table" then
                    -- we have not empty objects in our APIs
                    local body = cjson.encode(body):gsub("{}", "[]")
                    ngx.say(body)
                else
                    ngx.status = 502
                    ngx.say("Unexpected response from Tarantool")
                end
jobs-git commented 5 years ago

Ok, I got it displaying now as html, I created a pull request that includes this suggestion.

Totktonada commented 5 years ago

For the record: it is about PR #128.