Capture a request by lua nginx module - nginx

How to capture a request (ngx.req) in lua nginx module?(depend on method of request).
In lua nginx config, I need to check some conditions before send the request and return the response as normal, how can I do that?
I have used ngx.location.capture, but the method is different with method of ngx.req. I also use ngx.redirect(ngx.req) but it didn't work.

Just use the return statement to continue the request. return;

Related

Can we do regex for Response so that Consumer can have values dynamically?

I have written regex for response but the .json files are are creating with some wierd values like ABSGDKJEUDSGASH,
Can some help me in these
Response:
'agent': value(consumer((anyNonBlankString())), producer("abcd"))
Created Stubs
\"agent\":\"CVCHFTMETQSEOLOQENTY\":
Can spring cloud contract support response dynamically as it supports request??
consumer() is related to stubbing, producer() is related to the generated test. The matching in the request part is to make sure that the incoming HTTP request on your WireMock server matches the criteria that you have specified. Now everything that you write in the response side is what WireMock will return if your HTTP request matches those criteria.
If you need to return a fixed value, use consumer('my agent').
If it doesn't matter what you receive as a response, you can use eg. consumer(anyNonBlankString()).
If you want to return the same value as what you received from the client you can do something like consumer(fromRequest().header('agent')).
Hope that helps! :)

Post request handling with lua script in nginx

I use lua script to generate data from the parameters and bodies, and then send the data to the other server.
When i handle a GET request, nginx returns a normal response.
However, a 404 not found error occurs when handling POST requests.
However, internal operations were normal and sent the data to the other server.
Only the request method has changed.
If i explicitly pass the value to ngx.say or ngx.exit, i get 200 response normally.
Why? Is it necessary to explicitly return a response code when using a post request with a lua script?
In addition, I am using empty_gif.
I have searched for the above problem.
empty_gif can only be used to respond to GET and HEAD request methods
so I will use 204 response code

What is the difference between client HTTP request and server HTTP request in Golang's "net/http"

I have seen people use NewRequest() method of the "net/http" package for testing APIs. Why not use the NewRequest() method from "net/http/httptesting"? What's the difference? Documentation advises the following:
// To generate a client HTTP request instead of a server request, see
// the NewRequest function in the net/http package.
What would be the difference in handling cookies, for example? Both seem to be very similar.
TL;DR: they're the same type, used a bit differently for two use cases and initialized differently to serve these use cases
The difference is only in usage - they are the same type http.Request. http.NewRequest is used for the more "production" use case which is client - "create a new request to send to the server". When writing HTTP servers, it's occasionally useful to create requests for testing, which is what httptest.NewRequest does. The doc of http.NewRequest is helpful here:
NewRequest returns a Request suitable for use with Client.Do or
Transport.RoundTrip. To create a request for use with testing a Server
Handler, either use the NewRequest function in the net/http/httptest
package, use ReadRequest, or manually update the Request fields. See
the Request type's documentation for the difference between inbound
and outbound request fields.
If you check the docs of the http.Request type, you'll find things like:
// URL specifies either the URI being requested (for server
// requests) or the URL to access (for client requests).
//
// For server requests, the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 7230, Section 5.3)
//
// For client requests, the URL's Host specifies the server to
// connect to, while the Request's Host field optionally
// specifies the Host header value to send in the HTTP
// request.
URL *url.URL
Note the "For client requests" vs. "For server requests".
If you see a place that doesn't use httptest.NewRequest it could be because:
They're not aware of it
Or they need more careful fine-tuning that http.NewRequest doesn't provide

NGINX : Making two asynchronously calls to a backend server

I want to do the following using NGINX Module :
Nginx receives a request, checks if it has the key to decode the request in the cache(custom)
if YES, then decode request, obtain an ID from it and check if there is a value against this ID in a key-value store (asynchronously) and return it in the response
if NO, then get the new key from the key-value store (asynchronously) and then store this key in the cache and use it to decode the request. Obtain the ID and check if there is a value against this ID in the key-value store(asynchronously) and send it in the response.
I was able to figure out how to do step 1, i wrote an upstream module by referring openresty's nginx module from github. For achieving step 2 functionality, i tried creating a new upstream request in the process_header() function of the first upstream call (i.e getting the key from the store), but this didn't work. How to achieve this ?
Thanks in advance.
I see 2 approaches:
You may do it all in Lua using lua-nginx-module and lua-resty-redis library. Here you may find some info Configure-nginx-to-get-url-from-redis-with-key-and-proxy-the-url-to-other-server
Write nginx C module, use redis2-nginx-module as upstream module, send subrequest. Take a look at my answer to Subrequests are not sent or the request hangs It shows how to send subrequests.

Why sitebricks response with 405 status when using delete method?

I am implementing a simple rest service with the 4 http methods get,post, put and delete using sitebricks. trying to send a delete request to the defined service with a WebClient I get a 405 response. Does anyone knows why would I get such response ?
10:22:24.840 [5907955#qtp-6711891-2 - /r/clients/123] DEBUG org.mortbay.log - RESPONSE /r/clients/123 405
This is how I use web client
WebClient client = web().clientOf(delete(123)).transports(String.class).over(Json.class);
client.delete();
here is my delete method
#Delete
#At("/:id")
public Reply delete(#Named("id") String id) {
clientsRepository.delete(id);
return Reply.saying().ok();
}
I am using Jetty Server.
Response code 405 means that something is configured to not allow the use of the http DELETE method.
I can't speak for sitebricks itself, but servlet spec allows you to disable specific methods.
The web.xml of your webapp or the ${jetty.home}/etc/webdefault.xml, as either could be configured to disallow the use of specific HTTP methods (like TRACE, PUT, DELETE).
Check those files for <security-constraints> that might have <http-method> declarations for DELETE.
Also note that any code can trigger the 405 response itself.
Since you are seeing this in Sitebricks, possibly a <filter> in your web.xml is preventing it.
Are you constructing the request URI properly? Sitebricks returns 405 if there is no handler method. This test case verifies that #Delete does indeed work properly:
https://github.com/dhanji/sitebricks/blob/master/sitebricks-acceptance-tests/src/main/java/com/google/sitebricks/example/RestfulWebServiceWithSubpaths2.java
Also as joakime says, do check if any other filters or handlers are firing outside Sitebricks.

Resources