[英]Sending POST request in ESB mule
我嘗試使用ESB ule子將發布請求發送到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代碼段如下。
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中發送后有效載荷(fetchTokenRequest)。 我如何在m子ESB中發送相同的有效載荷。
如果它是流發布有效負載,如何在ESB Mule中發送流發布有效負載?
在您的示例中,您的HTTP偵聽器正在偵聽的任何有效負載都將是您用於發布請求的有效負載。 要使用其他有效負載,請在出站http之前使用<set-payload doc:name="Set Payload" value="your_value"/>
。
將Content-Type
設置為application/json
set-payload
,使用<json:object-to-json-transformer/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.