【问题标题】:Sending POST request in ESB mule在 ESB mule 中发送 POST 请求
【发布时间】:2016-03-09 15:33:12
【问题描述】:

我尝试使用 ESB mule 将发布请求发送到 API。因此,我创建了这样的流程。

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" 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" 
    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://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8110" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="api.bonanza.com" port="443" doc:name="HTTP Request Configuration"/>
    <flow name="bonanza_fetchtoken_ceFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/fetchtoken" allowedMethods="GET" doc:name="HTTP"/>
        <message-properties-transformer doc:name="Message Properties">
            <add-message-property key="X-BONANZLE-API-DEV-NAME" value="t*****I"/>
            <add-message-property key="X-BONANZLE-API-CERT-NAME" value="l*****F"/>
        </message-properties-transformer>
        <set-payload value="fetchTokenRequest" doc:name="Set Payload"/>
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/api_requests/secure_request" method="POST" doc:name="HTTP">
            <http:success-status-code-validator values="0..599"/>
        </http:request>
    </flow>
</mule>

在 API 文档中,他们提供了 java 发送和接收响应的示例代码,它也在我的本地工作。 java代码sn -p如下。

import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;

public class FetchToken {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            String devId = "t******I";
            String certId = "l*******F";

            URL url = new URL("https://api.bonanza.com/api_requests/secure_request");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("X-BONANZLE-API-DEV-NAME", devId);
            connection.setRequestProperty("X-BONANZLE-API-CERT-NAME", certId);

            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

            String requestName = "fetchTokenRequest";

            writer.write(requestName);
            writer.flush();
            writer.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String response = in.readLine();

            JSONObject jsonResponse = new JSONObject(response);

            if (jsonResponse.optString("ack").equals("Success") 
                    && jsonResponse.optJSONObject("fetchTokenResponse") != null) {
                // Success! Now read more keys from the json object
                JSONObject fetchTokenJson = jsonResponse.optJSONObject("fetchTokenResponse");

                System.out.println("Your token: " + fetchTokenJson.optString("authToken"));
                System.out.println("Token expiration time: " + fetchTokenJson.optString("hardExpirationTime"));
                System.out.println("Authentication URL: " + fetchTokenJson.optString("authenticationURL"));
            }


        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

我不是java专家。在上面的示例中,他们如何在 Java 中发送 post payload (fetchTokenRequest)。我如何在 mule ESB 中发送相同的有效负载。

如果是流后有效负载,我如何在 ESB mule 中发送流后有效负载?

【问题讨论】:

  • 嗨@Simbu,在mule anypoint studio 的调色板中有一个set-payload。使用它将有效负载设置为“fetchTokenRequest”。
  • 感谢您的回复。我已经尝试如下设置有效负载,但它会引发 API 错误。 &lt;set-payload value="fetchTokenRequest" doc:name="Set Payload"/&gt;

标签: java groovy mule esb


【解决方案1】:

在您的示例中,您的 http 侦听器正在侦听的任何有效负载都将是您发布请求的有效负载。要使用不同的负载,请在出站 http 之前使用 &lt;set-payload doc:name="Set Payload" value="your_value"/&gt;

Content-Type 设置为application/jsonset-payload之后,使用&lt;json:object-to-json-transformer/&gt;

【讨论】:

  • 我试过设置有效载荷。 &lt;set-payload value="fetchTokenRequest" doc:name="Set Payload"/&gt; 它给我一个 API 错误。在我试图从邮递员工具发出请求之间。这也会引发与 mule esb 相同的错误。在邮递员中,我在正文中设置为“原始”,并将内部的有效负载设置为“fetchTokenRequest”。不知道如何像上面的 java 程序一样发送有效载荷。
  • 我用过&lt;set-property propertyName="Content-Type" value="text/plain" doc:name="Property"/&gt;。也编辑了我的原始流程。
  • 这是API相关的错误。 { "ack": "Failure", "version": "1.0beta", "timestamp": "2016-03-09T09:53:50.000Z", "errorMessage": { "message": "Cannot determine what type of request you are making. Often this can be the result of data that has not been escaped before being passed to the API. If you are passing data with quotation marks or other special characters, you should translate it to JSON, then escape it, before sending it over the API." } }
  • 查看我的更新,外部服务需要application/json 而不是文本Content_type
  • 是的。我已经尝试过相同的 API 错误。在帖子中更新了我的 esb mule 流配置。此外,我已经在原始帖子中放置了完整的 java 程序。这个 java 程序为我返回令牌。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多