0

We're using Zuul as edge server. I want to write a filter that replaces the multipart/form-data from an inbound request with an entity which has the first application/json part of that request.

So that for example the request with multiparts:

[multipart/form-data]
[Part 1] << Application/JSON (name="info")
[Part 2] << Binary (name="file")

is translated into:

[application/json]
[Contents of Part 1]

Would this be possible with Zuul filters, and what type of filter should I use?

Pepster
  • 1,996
  • 1
  • 24
  • 41
  • what have you tried so far... also i think you will need a pre filter... so that you can modify the request before it goes downstream – Grinish Nepal Oct 23 '16 at 13:44
  • I have tried a pre-filter, but you can only enhance headers. I've found no way to manipulate the request body. I guess one approach is to override the default routing filter and hack a request body handler in, but that is not my preferred way of doing. – Pepster Oct 24 '16 at 07:42
  • I think the zuul requestContext has a way to modify the whole request. If you have written any code add that here so that someone can help you. Also check this out http://stackoverflow.com/questions/30400817/how-to-pass-modified-wrapped-httpservletrequest-to-subsequent-zuul-filters – Grinish Nepal Oct 24 '16 at 19:22

1 Answers1

0

I recently had to peek into the body to figure out how to route messages that were incoming. The code below shows how you can pull the body from the request and transform it into a JSON object. That might get you started.

public class ActivateServicePreFilter extends ZuulFilter {
@Override
public String filterType() {
    return PRE_TYPE;
}

@Override
public int filterOrder() {
    return 4;
}

@Override
public boolean shouldFilter() {

    HttpServletRequest request = RequestContext.getCurrentContext().getRequest();


    return "POST".equals(request.getMethod()) && request.getRequestURI().contains("uri-string");
}

@Override
public Object run() {

    HttpServletRequest request = RequestContext.getCurrentContext().getRequest();

    StringBuffer jb = new StringBuffer();
    String line = null;
    try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
            jb.append(line);
    } catch (Exception e) { /*report an error*/ }

    try {
        JSONObject jsonObject = new JSONObject(jb.toString());
        String jsonField = jsonObject.getString("jsonFieldKey");

        System.out.println(jsonField);

    } catch (JSONException e) {
        // crash and burn

    }




    return null;
}
Eric Goode
  • 81
  • 7