问题
I am trying to hit a rest api from my http-client processor module. I would like to know how can I send request header to the url I am trying to hit. Without the request header I get 500 Internal Server Error.
Will mappedRequestHeaders option be useful to pass headers, can someone give an example please.
Below is how my stream looks like :
jms --destination= | http-client --url='''https://hostname:11210/cards/accounts?eName=John%20Smith&caFSix=426600&caLF=1234''' --httpMethod=GET | log
回答1:
Yes, assuming your header is x-foo
, set the mappedRequestHeaders
property of the http-client
processor to "HTTP_REQUEST_HEADERS, x-foo"
.
Then, if the inbound JMS message has a header x-foo=bar
, it will be mapped in the http-client
.
This assumes you are using a local or rabbit message bus; for redis you would have to configure the bus to pass your header.
If the inbound JMS message doesn't have the header, you will need a custom processor (or a customized http-client
) to use a <header-enricher/>
to add the header.
EDIT:
You can use redis, but you need to add your header name to the servers.yml config for redis if you want them to traverse the bus:
xd:
messagebus:
redis:
headers:
However if you add a header-enricher directly in your custom http-client, they won't need to pass over the bus.
You can use json path to extract the content from your JMS message. If you also need to change the payload, you may find it easier to use a custom transformer to create the message.
Message<?> transform(Message<String> msg) {
return MessageBuilder.withPayload(newPayload)
.copyHeaders(msg)
.setHeader("accept", "...")
.setHeader(...)
. ...
.build();
}
EDIT#2:
I just tested it and it worked fine for me...
<header-enricher input-channel="input" output-channel="toHttp">
<header name="foo" value="bar" />
</header-enricher>
<channel id="toHttp" />
<int-http:outbound-gateway id='http-client'
request-channel='toHttp' url-expression="${url}" http-method="${httpMethod}"
expected-response-type='java.lang.String' charset='${charset}'
reply-timeout='${replyTimeout}' reply-channel='output'
mapped-request-headers="${mappedRequestHeaders}"
mapped-response-headers="${mappedResponseHeaders}">
</int-http:outbound-gateway>
<channel id="output" />
<channel id="input" />
With this stream definition...
xd:>stream create ticktock --definition "time --fixedDelay=5 | http-client --url='''http://localhost:8080/http/receiveGateway''' --mappedRequestHeaders=HTTP_REQUEST_HEADERS,foo | log --expression=#root" --deploy
...with these results...
18:02:20,284 INFO task-scheduler-3 sink.ticktock - GenericMessage [payload=2015-02-02 18:02:20 from the other side, headers={Server=Apache-Coyote/1.1, foo=bar, connection=keep-alive, id=8a444177-b96d-70c3-58e7-d92067d6b18e, Content-Length=39, contentType=text/plain, http_statusCode=200, Date=1422918140000, timestamp=1422918140284}]
18:02:25,292 INFO task-scheduler-3 sink.ticktock - GenericMessage [payload=2015-02-02 18:02:25 from the other side, headers={Server=Apache-Coyote/1.1, foo=bar, connection=keep-alive, id=d62b46ed-dcc7-6dd0-35ea-b7b988c4f2f1, Content-Length=39, contentType=text/plain, http_statusCode=200, Date=1422918145000, timestamp=1422918145292}]
As you can see, foo=bar
appears in the final message.
Now, in the HTTP message, by default, the user-defined headers get prefixed by X-
so the foo
header shows up as X-foo: bar
in HTTP.
In order to suppress the X-
, you need another tweak to the http-client
...
<header-enricher input-channel="input" output-channel="toHttp">
<header name="foo" value="bar" />
</header-enricher>
<channel id="toHttp" />
<int-http:outbound-gateway id='http-client'
request-channel='toHttp' url-expression="${url}" http-method="${httpMethod}"
expected-response-type='java.lang.String' charset='${charset}'
reply-timeout='${replyTimeout}' reply-channel='output'
header-mapper="mapper">
</int-http:outbound-gateway>
<beans:bean id="mapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
<beans:property name="userDefinedHeaderPrefix" value="" />
<beans:property name="outboundHeaderNames" value="${mappedRequestHeaders}" />
<beans:property name="inboundHeaderNames" value="${mappedResponseHeaders}" />
</beans:bean>
<channel id="output" />
<channel id="input" />
回答2:
@Gary As suggested by you, I did create a custom http-client with header-enricher as below :
<int-http:outbound-gateway id='http-client'
request-channel='headerenricheroutput' url-expression="${url}" http-method="${httpMethod}"
expected-response-type='java.lang.String' charset='${charset}'
reply-timeout='${replyTimeout}' reply-channel='output'
mapped-request-headers="${mappedRequestHeaders}"
mapped-response-headers="${mappedResponseHeaders}">
</int-http:outbound-gateway>
<channel id="output" />
<channel id="headerenricheroutput" />
<header-enricher input-channel="input" output-channel="headerenricheroutput">
<header name="User-Agent" value="Shred"/>
<header name="Api-Key" value="AbCdEFg1HiJ23klMnopQrS4U"/>
<header name="Accept" value="application/json"/>
</header-enricher>
<channel id="input" />
I created a stream as : stream create --name abc--definition "jms --destination=myQueue| http-client --url='''https://host:port/cards/accounts?eName=John%20Smith&cardFS=426600&cardLF=1234''' --mappedRequestHeaders=HTTP_REQUEST_HEADERS,User-Agent,Api-Key,Accept --httpMethod=GET | log" --deploy
But still I could not pass header to the URL. Can you please point me to correct direction.
Meanwhile, just for the sake of getting a reply from my API, I created a groovy script to invoke the above URL with header and I was able to do it. Maybe someone will find it useful so posting it. The groovy script is :
def json = "https://host:11210/credit/accounts?eName=John%20Smith&cardFS=426600&cardLF=1234".toURL().getText(requestProperties:['User-Agent': 'Shred', 'Api-Key': 'abc', Accept: 'application/json'])
The stream looks like :
jms --destination=tiki | transform --script=transform.groovy | log
Still I could not figure out what needs to be done exactly for http-client.
来源:https://stackoverflow.com/questions/28118990/passing-http-request-header-to-restapi-from-springxd-using-http-client-processor