Change post request body and then proxy pass

3,894 views
Skip to first unread message

Georgios Papageorgiou

unread,
Dec 15, 2017, 5:18:26 AM12/15/17
to Nginx-Clojure
Hello,

I want to create this scenario.

1)Client make a post request to nginx (e.g a json object)                                       !Done
2)Nginx clojure get the request with an NginxJavaRingHandler                             !Done
3)Nginx clojure change the body of the request(e.g add a new property to json)   !Done
4)Set the new body to request                                                                                !Done
5)Send the new body to proxy pass...                                                                     !Not done. Every time the first request body is passed to proxy. Is like the 4th step not                                                                                                                                         take  effect.

Above are the rewrite handler i use and the nginx.conf.

public class HelloGroovy implements NginxJavaRingHandler {
private static final String HOST_NAME = "{my proxy pass url to make the post}"
private String uri = "";
public Object[] invoke(Map<String, Object> request){
uri = (String) request.get("uri")
NginxJavaRequest r = (NginxJavaRequest) request

String enchancedJson = getEnchancedJson((InputStream) request.get("body"))
/**
* Important...
* Those are the same ? 
* 1) r.put(NginxClojureRT.BODY, enchancedJson)
* 2) r.setVariable(NginxClojureRT.BODY, enchancedJson)
*/
r.put(NginxClojureRT.BODY, enchancedJson)
String url = HOST_NAME.concat(uri)
r.setVariable("mytarget", url)

return PHASE_DONE
}
def getEnchancedJson(InputStream input){
if (input != null) {
def jsonRequest = input.text
def slurper = new JsonSlurper()
def result = slurper.parseText(jsonRequest)
def builder = new JsonBuilder()
def root = builder.test{
id  "1"
time new Timestamp(System.currentTimeMillis())
}
result.test = root.test
return new JsonBuilder(result).toPrettyString()
}
return null
}
}

listen 8080;
server_name localhost;
set $mytarget "";  

location / {
  resolver 8.8.8.8;
  always_read_body on;
                  rewrite_handler_type 'groovy';
                  rewrite_handler_name 'gr.test.HelloGroovy';
  proxy_pass $mytarget;
}

Thanks in advance for the help.

Yuexiang Zhang

unread,
Dec 15, 2017, 10:52:44 AM12/15/17
to Georgios Papageorgiou, Nginx-Clojure
Hi, rewrite handle can not change request body directly but we can do it by nginx variable and proxy_set_body;

e.g.

set $mytarget "";
set $mybody "";  

location / {
  resolver 8.8.8.8;
  always_read_body on;
                  rewrite_handler_type 'groovy';
                  rewrite_handler_name 'gr.test.HelloGroovy';
                  proxy_set_body $mybody;
  proxy_pass $mytarget;
}

In rewrite handler 

r.setVariable("mybody", new-body-here);


--
You received this message because you are subscribed to the Google Groups "Nginx-Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nginx-clojure+unsubscribe@googlegroups.com.
To post to this group, send email to nginx-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nginx-clojure/1719b4c7-cb8e-43e6-a022-09de2df2b15a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Georgios Papageorgiou

unread,
Dec 18, 2017, 2:47:10 AM12/18/17
to Nginx-Clojure
Thanks.... that was what i was looking for...
To unsubscribe from this group and stop receiving emails from it, send an email to nginx-clojur...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages