openresty how to deal with the http code in restful webservice

139 views
Skip to first unread message

whs860603

unread,
Aug 11, 2014, 10:13:41 PM8/11/14
to openre...@googlegroups.com
 I am newbie here,and try to develop a restful webservice based on the openresty module,perhaps lua-resty-http is the best one .Is there any suggestion or best practise about how to deal with the following http code in lua-resty-http or openresty?have anyone try to use the RAML or Swagger spec for the restful interface with openresty?
  • Success: HTTP 200, XML/JSON representation of object (when applicable)
  • Invalid: HTTP 422, XML/JSON representation of errors
  • Unauthorized: HTTP 401
  • Missing: HTTP 404
  • Forbidden: HTTP 403
  • Method not Allowed: HTTP 405
  • Conflict: HTTP 409
  • .....

Jon Keys

unread,
Aug 11, 2014, 11:10:50 PM8/11/14
to openre...@googlegroups.com


On Monday, August 11, 2014 10:13:41 PM UTC-4, whs860603 wrote:
 I am newbie here,and try to develop a restful webservice based on the openresty module,perhaps lua-resty-http is the best one .Is there any suggestion or best practise about how to deal with the following http code in lua-resty-http or openresty?have anyone try to use the RAML or Swagger spec for the restful interface with openresty?

lua-resty-http is an HTTP client. Are you trying to consume a RESTful web service?

I've had very good luck using ngx.location.capture
You can create an internal location which proxies to an upstream server (including HTTPS hosts).

For example:

http {
...
    upstream api_host {
        server api.host.com:443;
        keepalive 4;
    }

    server {
    ... 
        location /_api_proxy {
            internal;
            proxy_pass https://api_host;
            proxy_pass_header Server;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_set_header Host api.host.com;
        }

        location / {
            content_by_lua '
                local res = ngx.location.capture("/_api_proxy")
                ...
            ';
        }
    }
}

        

If you're trying to create a RESTful web service then you have a lot of choices.
For creating a dynamic response body you might look at content_by_lua. The cjson module is especially useful for serializing to JSON text and it is already included in the OpenResty bundle.
You can easily set the correct status using ngx.status (also see HTTP status constants).

If you can tell us a bit more about your requirements we might be able to offer more assistance.

whs8...@gmail.com

unread,
Aug 11, 2014, 11:21:40 PM8/11/14
to openre...@googlegroups.com
thanks a lot.I am trying to create a RESTful web service.




--
王海生
15901958021

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages