gpt4 book ai didi

java - 在 ESB mule 中为 API 发送 POST 请求

转载 作者:行者123 更新时间:2023-11-30 07:32:26 24 4
gpt4 key购买 nike

我尝试使用 ESB mule 将 post 请求发送到 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" host="api.bonanza.com" port="443" doc:name="HTTP Request Configuration" protocol="HTTPS"/>
<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="text/plain" doc:name="Property"/>
<set-property propertyName="Accept" value="application/json" doc:name="Property"/>
<http:request config-ref="HTTP_Request_Configuration" path="/api_requests/secure_request" method="POST" doc:name="HTTP" followRedirects="true" parseResponse="false">
<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程序重新调整 token 没有问题。当我使用我的 esb mule 时,出现以下 API 错误。我在构建请求时犯了哪些错误?我怎样才能从 API 获得成功的响应?

{
"ack": "Failure",
"version": "1.0beta",
"timestamp": "2016-03-09T10:35:16.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."
}
}

最佳答案

Solving your problem implies performing debugging steps. I'm going to walk you through them.

假设:也许接收服务被 Mule 发送的一些额外 header 混淆了?

尝试以下操作:

  • 将 Mule 请求发送至 http://requestb.in/并准确查看它的结构。
  • 然后从 Java 向 api.bonanza.com 发送相同的请求,它也应该失败。
  • 一点一点地删除多余的东西,直到它起作用为止。
  • 然后返回 Mule,通过调整发送的 header 、修复任何其他差异来确保它创建相同的请求...

关于java - 在 ESB mule 中为 API 发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35900019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com