4

I have a proxy that sits in between a client and a server that are communicating using json objects. The proxy needs to handle the data as a string. It has no knowledge of the types it simply needs to scan the data for strings. The problem is when I forward the data to the server from the proxy the string is quoted and the quotes in the string are escaped. I need to send the string exactly as is.

@Headers({"Accept: application/json"})
interface ExampleClient {

   @Headers({"Content-Type: application/json"})
   @RequestLine("POST /examples/postTest")
   Response postTest(String body);
}

The string is

{"name":"Alfred","nickname":"Alfy","number":45}

but in the body of the post it is

"{\"name\":\"Alfred\",\"nickname\":\"Alfy\",\"number\":45}"

This occurs regardless of what the content type is set to.

Is there anyway I can post a string using feign without it being qouted?

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
Jeff Gaer
  • 351
  • 3
  • 21
  • How do you see this escaped quotation? In your IDE? Or when you do `System.out.println(serializedBody);`? That looks like the IDE's representation of the non-escaped string to me. – Shadow Man Apr 12 '18 at 22:01
  • If this is the actual output of the serialized body via `System.out.println` then you can likely fix this by passing in the non-serialized object rather than an already-serialized string. My guess would be that the `JacksonEncoder` is taking your string and encoding it into JSON because it doesn't realize that it's already encoded and you specified `"Content-Type: application/json"` in your headers. Or maybe try using your own pass-through `Encoder` that just returns the value if it is already a `String`. – Shadow Man Apr 12 '18 at 22:04
  • I have a tool that sits between the client and server that displays the raw http stream, headers and all. The server expects json objects, but the proxy operates on strings. So the idea was to forward the request on as a string from the proxy's point of view, but have the server interpret it as json. I think feign decides to quote the string because of the content-type. I wound up just using apache's http client. – Jeff Gaer Apr 14 '18 at 02:03
  • Then it sounds like Feign is not what you want. You probably want a simple proxy instead. Perhaps something like: https://github.com/mitre/HTTP-Proxy-Servlet – Shadow Man Apr 17 '18 at 03:14
  • 2
    You could also just write a pass-through encoder that casts the body object and returns it like: `return (String)object;` (assuming all of your endpoints use the string passthrough) with `Feign.builder().encoder(new MyPassThroughEncoder()).target(MyApi.class, myRootUri)`. But I really think Feign is the wrong tool for what you want to do. – Shadow Man Apr 17 '18 at 03:16

1 Answers1

2

In my code

GsonDecoder decoder = new GsonDecoder();
                client = Feign.builder()                
                **.encoder(new GsonEncoder())**
                .logLevel(feign.Logger.Level.FULL)
                .logger(new Slf4jLogger(CampaignsClient.class))
                .requestInterceptor(template -> template.header(AUTHORIZATION_HEADER, "Bearer ..."))
                .target(CampaignsClient.class, "https://server");

I remove line of code .encoder(new GsonEncoder()) and feign stop string escaping.

Dmitry
  • 536
  • 1
  • 4
  • 9