我正在使用http header enricher组件来传递标头,但我无法使用http-client发出http请求
我有一个像下面这样的流我正在尝试创建一个http rest Post请求并将其记录到logger。我有一个API-Key标头,我在下面使用了这个标题更丰富的模块 https://github.com/spring-projects/spring-xd-modules/tree/master/header-enricher
stream create --name test24 --definition "http |header-enricher --headers={\"Api-Key\":\"'xyz'\"}| http-client --url='''http://xyz:/abc''' --httpMethod=POST | log " --deploy
http post --data '{"profileReferenceID":"pPPpe85Ht91e%2FpCvlkJMkniwiM%2BGvPjATkjc6HGqaJFS065txVj%2BhqDPsHz54KEm1RrCXftjNCSl2fpBKKKdrie9n6t9jetWhk29ELCmbw0%3D","templateURL":{"type":"type","href":"/content/notification/templates/EOSWEL?firstName=Abcd&userName=dddd&accountLast=2144&phone=1234&SvcgLOBCd=MS","method":"POST"}}'
> POST (text/plain;Charset=UTF-8) http://localhost:9000 {"profileReferenceID":"pPPpe85Ht91e%2FpCvlkJMkniwiM%2BGvPjATkjc6HGqaJFS065txVj%2BhqDPsHz54KEm1RrCXftjNCSl2fpBKKKdrie9n6t9jetWhk29ELCmbw0%3D","templateURL":{"type":"type","href":"/content/notification/templates/EOSWEL?firstName=Abcd&userName=dddd&accountLast=2144&phone=1234&SvcgLOBCd=MS","method":"POST"}}
2015-11-16T13:05:24-0600 1.2.1.RELEASE WARN xdbus.test24.1-1 retry.RejectAndDontRequeueRecoverer - Retries exhausted for message (Body:'{"profileReferenceID":"pPPpe85Ht91e%2FpCvlkJMkniwiM%2BGvPjATkjc6HGqaJFS065txVj%2BhqDPsHz54KEm1RrCXftjNCSl2fpBKKKdrie9n6t9jetWhk29ELCmbw0%3D","templateURL":{"type":"type","href":"/content/notification/templates/EOSWEL?firstName=Abcd&userName=dddd&accountLast=2144&phone=1234&SvcgLOBCd=MS","method":"POST"}}'MessageProperties [headers={requestMethod=POST, User-Agent=Java/1.8.0_60, Host=localhost:9000, Content-Length=305, contentType=text/plain, Api-Key=xyz, requestPath=/, originalContentType=text/plain;Charset=UTF-8}, timestamp=null, messageId=null, userId=null, appId=null, clusterId=null, type=null, correlationId=null, replyTo=null, contentType=text/plain, contentEncoding=null, contentLength=0, deliveryMode=null, expiration=null, priority=0, redelivered=false, receivedExchange=, receivedRoutingKey=xdbus.test24.1, deliveryTag=2, messageCount=0])
我得到403禁止我从我的webservice获得此错误,如果我的客户端api密钥没有通过,我能够在post man中重现。所以我认为我的API-Key标头没有通过。
caused by: org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://xyx/abc]; nested exception is org.springframework.web.client.HttpClientErrorException: 403 Forbidden
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:410) ~[spring-integration-http-4.1.6.RELEASE.jar:na]
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:287) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:245) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) ~[spring-messaging-4.1.7.RELEASE.jar:4.1.7.RELEASE]
答案 0 :(得分:1)
您应该添加要映射到HTTP请求标头的Api-Key
标头:
http-client --url='''http://xyz:/abc''' --httpMethod=POST --mappedRequestHeaders=HTTP_REQUEST_HEADERS,Api-Key
mappedRequestHeaders
request message header names to be propagated to/from the adpater/gateway (String, default: HTTP_REQUEST_HEADERS)
其中HTTP_REQUEST_HEADERS
是一组仅适用于请求的标准HTTP标头。
http://docs.spring.io/spring-xd/docs/current/reference/html/#http-client