首页 > 解决方案 > Set Content-type Apache NiFi dynamically

问题描述

I'am using Apache NiFi for a small webservice that should be able to serve static files(HTML, JavaScript, PNG, SVG, CSS). I have encountered an issue since NiFi not seems to set the Content-Type in the response header.

How can I configure the HandleHttpResponse-module to set the Content-Type header properly based on the requested filetype?

One way to do it is to use nested ifElse-statements in Expression Language but I don't think that would be best practice?

What I'am trying to achieve is that it should set Content-Type to image/svg+xml if the requested file is an SVG file, text/css if the requested file is an CSS-file etc.

Thanks in advance!

标签: apache-nifi

解决方案


使用LookupAttribute处理器

将文件扩展名和 Content-Type 的映射放入 SimpleKeyValueLookupService

svg = image/svg+xml
css = text/css
...

配置 LookupAttribute 以使用 SimpleKeyValueLookupService 并设置动态属性:

mime.type = ${filename:replaceAll('.*\\.([^\\.]+)$','$1')}

推荐阅读