spring-cloud-openfeign icon indicating copy to clipboard operation
spring-cloud-openfeign copied to clipboard

Dynamically set Content-Type via @RequestHeader

Open sixcorners opened this issue 8 years ago • 8 comments
trafficstars

I want to set the Content-Type header programatically. When I try this happens:

Caused by: feign.codec.EncodeException: Invalid mime type "{Content-Type}": does not contain '/'
	at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:346)
	at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:213)
	at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:72)
	at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:108)
	at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:301)
	at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:297)
	at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
	... 27 common frames omitted
Caused by: org.springframework.http.InvalidMediaTypeException: Invalid mime type "{Content-Type}": does not contain '/'
	at org.springframework.http.MediaType.parseMediaType(MediaType.java:425)
	at org.springframework.http.MediaType.valueOf(MediaType.java:410)
	at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:64)
	at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:342)
	... 33 common frames omitted
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "{Content-Type}": does not contain '/'
	at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:239)
	at org.springframework.http.MediaType.parseMediaType(MediaType.java:422)
	... 36 common frames omitted
@FeignClient("x")
public interface XClient {
	@RequestMapping(value = "/api/something/{id}", method = POST)
	void x(@RequestHeader("Content-Type") String contentType, @PathVariable("id") Integer id,
		@RequestBody InputStream inputStream);
}

I ended up having to set a X-Content-Type2 header and setting up one of these:

@Bean
public RequestInterceptor contentTypeFilter() {
	return (req) -> {
		req.header("Content-Type", req.headers().get("X-Content-Type2").iterator().next());
	};
}

I am using: spring-cloud-netflix-core-1.2.5.RELEASE.jar

sixcorners avatar May 01 '17 18:05 sixcorners

I dont believe you can set it programmatically using @RequestHeader, I believe we only support doing it via the consumes property of @RequestMapping.

ryanjbaxter avatar May 01 '17 18:05 ryanjbaxter

Is it possible to add support? Would it be a good idea to feed it to the @RequestMapping annotation using SpEL?

sixcorners avatar May 06 '17 19:05 sixcorners

I have same problem. Can I know some due date for this issue? My FeignClient request need to resend some header param from client application so I need dynamic value of header param. Is some other option for this case? Thanks.

thomasharin avatar Jul 27 '17 20:07 thomasharin

@thomasharin no due date. You can always use a RequestInterceptor.

spencergibb avatar Aug 04 '17 20:08 spencergibb

This would have to be changed inside Spring MVC, am I right?

thorstenfrank avatar Oct 19 '17 21:10 thorstenfrank

Did you find any solution for it?

alvessergio avatar Nov 30 '18 12:11 alvessergio

+1

juliofalbo avatar Aug 08 '19 19:08 juliofalbo

\o

raizoor avatar Jan 17 '20 18:01 raizoor