Spring XD http-client processor sends text/plain instead of application/json

438 views Asked by At

I am trying to create a stream that sends a POST request as part of a stream.

stream create form-time --definition "<working source> | generate-form-transformer: transform --script=form-generation.groovy  --outputType=application/json | http-client --inputType=application/json ..." --deploy

It works, but it sets contentType to text/plain instead of application/json, which provokes a 400 since my server does not support that. (The content is perfect JSON though).

The transformer returns a XD Tuple, so as far as I understood conversion from Tuple to JSON should be done properly (in fact, as pointed out previously, the payload is correct).

However, when it comes to http-client it does not fix contentType being text/plain;UTF-8 instead of application/json.

Any suggestions? Am I missing something? Thank you.

2

There are 2 answers

3
Gary Russell On BEST ANSWER

I don't believe the --inputType and --outputType conversions are supposed to set a header, they just perform the conversion; one technique would be to add a header enricher to the http-client...

$ git diff
diff --git a/modules/processor/http-client/config/http-client.xml b/modules/processor/http-client/config/http-client.xml
index cf7d2e2..c2ecd00 100644
--- a/modules/processor/http-client/config/http-client.xml
+++ b/modules/processor/http-client/config/http-client.xml
@@ -10,7 +10,7 @@
                        http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

        <int-http:outbound-gateway id='http-client'
-               request-channel='input' url-expression="${url}" http-method="${httpMethod}"
+               request-channel='inputX' url-expression="${url}" http-method="${httpMethod}"
                expected-response-type='java.lang.String' charset='${charset}'
                reply-timeout='${replyTimeout}' reply-channel='output'
                mapped-request-headers="${mappedRequestHeaders}"
@@ -19,5 +19,9 @@

        <channel id="output" />
        <channel id="input" />
+       <channel id="inputX" />
+       <header-enricher input-channel="input" output-channel="inputX">
+               <header name="content-type" value="application/json" />
+       </header-enricher>

There is now a header-enricher processor module in spring-xd-modules.

0
Ilayaperumal Gopinathan On

stream create form-time --definition " | generate-form-transformer: transform --script=form-generation.groovy --outputType=application/json | http-client --inputType=application/json ..." --deploy

Did you set --outputType=application/json for the http-client module. Also, the --inputType in http-client isn't necessary as the output type of the previous module is already application\json. Probably, you intend to set that as --outputType?