Ajv2324
Ajv2324

Reputation: 522

Can't perform an HTTP POST in Mule when sending JSON object as payload

This is a problem I wasn't facing previously when I was trying something simpler. I'm trying to use Mule to send messages to Slack, and in doing so I first tried to just send a JSON object to the hook.slack.com address for my specific slack group. I did this and it worked exactly as intended (though obviously the JSON is just something static on the ESB). This looked like:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="hooks.slack.com" doc:name="HTTP Request Configuration"/>
    <flow name="testFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-payload value="{&quot;channel&quot;: &quot;#general&quot;, &quot;username&quot;: &quot;test&quot;, &quot;text&quot;: &quot;How's this?&quot;, &quot;icon_emoji&quot;: &quot;:man_with_turban:&quot;}" doc:name="Set Payload"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/services/myslackaddress" method="POST" doc:name="HTTP"/>
    </flow>
</mule>

And as mentioned this worked fine- slack would get the JSON and use it as intended to post a message into the given group. I then expanded upon this ever so slightly by creating an ESB that takes a IMAP connector, gets any email that's sent to it, and pastes the body of that email into slack. This seemed like a simple enough change, and what I've done looks like this (please ignore the logs, they're just for debugging obviously):

<imaps:connector name="IMAP" validateConnections="true" checkFrequency="300" doc:name="IMAP">
</imaps:connector>
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="hooks.slack.com"  doc:name="HTTP Request Configuration"/>
<flow name="Global_ResourcesFlow">
    <imaps:inbound-endpoint host="imap.gmail.com" port="993" user="testemail" password="password" connector-ref="IMAP" responseTimeout="10000" doc:name="Grab email sent to testemail"/>
    <email:email-to-string-transformer doc:name="Convert body of email to string"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    <set-variable variableName="emailBody" value="#[payload]" doc:name="Set body text"/>
    <set-payload value="{&quot;channel&quot;: &quot;#general&quot;, &quot;username&quot;: &quot;Bot&quot;, &quot;text&quot;: &quot;#[flowVars.emailBody]&quot;, &quot;icon_emoji&quot;, &quot;:heart_eyes_cat:&quot;}" doc:name="Set Payload"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    <http:request config-ref="HTTP_Request_Configuration" path="/services/myslackaddress" method="POST" doc:name="HTTP">
        <http:request-builder>
        </http:request-builder>
    </http:request>
</flow>

This seems like it's just a small change to me but for some reason now when I access that final http endpoint I get some sort of error in the Mule console that looks like this:

ERROR 2015-06-09 12:32:47,462 [[slack-notifications-esb].Global_ResourcesFlow.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Response code 500 mapped as failure. Message payload is of type: BufferInputStream
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Response code 500 mapped as failure. Message payload is of type: BufferInputStream (org.mule.module.http.internal.request.ResponseValidatorException)
  org.mule.module.http.internal.request.SuccessStatusCodeValidator:37 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/http/internal/request/ResponseValidatorException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.module.http.internal.request.ResponseValidatorException: Response code 500 mapped as failure. Message payload is of type: BufferInputStream
    at org.mule.module.http.internal.request.SuccessStatusCodeValidator.validate(SuccessStatusCodeValidator.java:37)
    at org.mule.module.http.internal.request.DefaultHttpRequester.innerProcess(DefaultHttpRequester.java:202)
    at org.mule.module.http.internal.request.DefaultHttpRequester.process(DefaultHttpRequester.java:166)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

It's even weirder because the final payload that is being sent is the same payload that is being sent in the other project that is working fine, which is just some valid JSON that looks like:

{"channel": "#general", "username": "Bot", "text": "test", "icon_emoji", ":heart_eyes_cat:"}

Upvotes: 2

Views: 8461

Answers (1)

David Dossot
David Dossot

Reputation: 33413

Add:

<http:header headerName="Content-Type" value="application/json" />

in http:request-builder to specify that the request body is a JSON entity.

Upvotes: 3

Related Questions