Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feign - URL encode path params

This is my contract,

@RequestLine("GET /products/{id}")
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;

I want to fetch the product with id = "a/b",

If I send this as a param to getProduct("a/b")

then the URL that is formed is http://api/products/a/b and I am getting a 404 instead the url should be http://api/products/a%2Fb

Is there a way around this?

like image 743
NitishDeshpande Avatar asked May 10 '17 09:05

NitishDeshpande


Video Answer


2 Answers

A simple config did it,

@RequestLine(value = "GET /products/{id}", decodeSlash = false)
@Headers({"Content-Type: application/json"})
ApiResponse getProduct(@Param("id") String productId) throws Exception;

The path param was correctly getting encoded but the RequestTemplate was decoding the URL again (decodeSlash=true by default) before sending out the request which was causing the issue.

like image 186
NitishDeshpande Avatar answered Sep 24 '22 15:09

NitishDeshpande


In my case, when code looks like this:

@GetMapping(path = "/document/{documentId}/files/{fileId}")
  ResponseEntity<byte[]> getDocument(@PathVariable("documentId") String documentId, @PathVariable(value = "fileId") String fileId);

Also problem was that @PathVariable fileId could be 123/SGINED.

Setting application.property feign.client.decodeSlash=false helped.

like image 22
Raba_Ababa Avatar answered Sep 25 '22 15:09

Raba_Ababa