rest - 如何在 apache camel 中使用基本身份验证进行 RESTful 调用?
问题描述
我有一个需要将日志文件发送到端点的 apache camel 应用程序,这需要基本身份验证。我能够将 authMethod、authusername 和 authPassword 传递给骆驼文档中指定的 url,但我面临的挑战是在启动应用程序后我不断从端点获得空响应。但是,同一端点使用邮递员返回响应代码和响应正文。
下面是我的代码:
from("{{routes.feeds.working.directory}}?idempotent=true")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
String fileName = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
File file = exchange.getIn().getBody(File.class);
multipartEntityBuilder.addPart("file",
new FileBody(file, ContentType.MULTIPART_FORM_DATA, fileName));
exchange.getOut().setBody(multipartEntityBuilder.build());
Message out = exchange.getOut();
int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
log.info("response code "+responseCode);
}
})
.setHeader(Exchange.HTTP_QUERY,
constant("authMethod=Basic&authUsername="+username+"&authPassword="+password+""))
.to(TARGET_WITH_AUTH +"/"+uuid+"/files")
.log(LoggingLevel.DEBUG, "response code >>>>"+Exchange.HTTP_RESPONSE_CODE)
.log(LoggingLevel.INFO, "RESPONSE BODY ${body}")
.end();
请帮助审查和进一步建议
解决方案
对于 HTTP 基本身份验证,我在发送请求之前使用它
<setHeader headerName="Authorization">
<constant>Basic cm9vdDpyb290</constant>
</setHeader>
cm9vdDpyb290 - 编码的 Base64 root:root(用户名和密码)字符串
推荐阅读
- javascript - 在 Scrollview 中查看不显示
- node.js - 节点 ImageMagick 在本地成功但在 AWS Lambda 中失败
- mysql - 加入两个表后计数
- javascript - iife 内部函数需要明确的这个赋值
- tensorflow - 在 model_config_file 中分配版本标签失败
- python - 将 `tf.constant` 与整数进行比较
- c# - mdb文件数据插入增加mdb大小
- r - Concatenate data.frame character columns based on column index stored as a vector in R w/ dplyr mutate()?
- python - Python将图像堆叠成立方体
- c# - 使用 RegEx 在文本 c# 中查找 100$