簡體   English   中英

通過使用Mulesoft的Web API獲得“響應代碼404映射為失敗”

[英]getting “Response code 404 mapped as failure” by consuming web api from Mulesoft

我正在嘗試使用開放源代碼api,將響應代碼404映射為失敗。

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="api.taxjar.com" port="443" basePath="v2/taxes" doc:name="HTTP Request Configuration"/>
<flow name="postTaxCollectionFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/taxcollection" allowedMethods="POST" doc:name="HTTP"/>
    <dw:transform-message doc:name="Transform Message" metadata:id="2789cbd2-5ca6-46c2-856f-67ba2bdfa6dd">
        <dw:input-payload mimeType="application/json"/>
        <dw:set-payload>
            <![CDATA[%dw 1.0
            %output application/json
            ---
            payload
            ]]></dw:set-payload>
    </dw:transform-message>
    <http:request config-ref="HTTP_Request_Configuration" path="https://api.taxjar.com/v2/taxes" method="POST" doc:name="Web Service">
        <http:request-builder>
            <http:query-param paramName="Authorization" value="Token token=&quot;8dbc821e651fe0672c4032e65209b37c&quot;"/>
            <http:query-param paramName="Content-Type" value="application/json"/>
        </http:request-builder>
    </http:request>
    <byte-array-to-object-transformer doc:name="Byte Array to Object"/>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

錯誤消息是

響應代碼404映射為失敗。 有效負載:org.glassfish.grizzly.utils.BufferInputStream@12c30f42元素:/ postTaxCollectionFlow / processors / 1 @ taxcollection_apisero:taxcollection_apisero.xml:23(Web服務)元素XML:http:請求構建器

請幫助我進行配置

造成404錯誤的原因是您的http:request連接器構造的URL錯誤。

從HTTP請求連接器配置的MuleSoft文檔中

You need to provide a path and method for your requests, as well as reference 
a Connector Configuration global element. Note that the path field doesn’t 
define the full path, but rather the subpath, within the host and after the 
optional base path that can be specified in the Connector Configuration global element.

使用此邏輯,您嘗試從中請求數據的代碼中構造的URL是https://api.taxjar.com:443/v2/taxes/https://api.taxjar.com/v2/taxes

嘗試從Postman / RestClient中訪問所需的URL,看看是否給出有效的響應。

我試圖復制發布了一些虛擬數據的流,似乎很成功

https://api.taxjar.com/v2/taxes/taxcollection ”網址

這給出了以下回應

{
"error": "Not Found",
"detail": "No such route 'POST /v2/taxes/taxcollection'",
"status": "404"
}

如果它可以在郵遞員中使用,則應該與Mule一起使用。如果它在郵遞員中可以正常工作,而不是在m子中工作,則將請求數據和url與您的m子流一起發布(如果可能的話),這將有助於我們找出根本原因

檢查Webservice組件中的路徑,因為您已經指定了主機和端口,所以不必將該路徑設置為完整路徑。 請更改路徑並檢查響應。

完全同意Jason Estevan的回答。 您的HTTP出站連接器的配置應如下所示:

<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="api.taxjar.com" port="443" doc:name="HTTP Request Configuration"/>

出站連接器應如下所示:

<http:request config-ref="HTTP_Request_Configuration" path="v2/taxes" method="POST" doc:name="Web Service">
        <http:request-builder>
            <http:query-param paramName="Authorization" value="Token token=&quot;8dbc821e651fe0672c4032e65209b37c&quot;"/>
            <http:query-param paramName="Content-Type" value="application/json"/>
        </http:request-builder>
    </http:request>

在路徑中,只有網址的路徑應該在主機名和端口之后。 由於您已經在連接器配置中定義了主機名和端口,因此路徑中不應存在該主機名和端口。

檢查同一端口上是否有另一台服務器(即使聽起來太奇怪以至於無法實現)。

在端口8082和JavaSlurper也都在相同的端口8082中擁有Java Spring服務器時,我們遇到了相同的錯誤。使用curl調用API總是成功觸發了Spring應用程序中的斷點:

curl -X POST -H "Content-Type: application/json" -D - -d '{}' http://localhost:8082/api/foobar

從Mule 3.8.1 / 3.9.0獨立調用API導致可怕的“響應代碼404映射為失敗”。 我們通過調用服務器根目錄來找到解決方案,如下所示:

curl -X GET -H "Content-Type: application/json" -D - -d '{}' http://localhost:8082

然后,我們從MailSlurper實例得到了令人驚訝的響應,該實例將其首頁html發送回了。 之后,結束MailSlurper進程,更改其端口並重新啟動很簡單。

編輯:

如果端口上沒有服務器,則卷曲輸出:

curl: (7) Failed to connect to localhost port 8082: Connection refused

如果您的服務器運行正常,但根路徑未配置為在GET上返回任何內容:

$ curl -X POST -H "Content-Type: application/json" -D - -d '{}' 
http://localhost:8082/
HTTP/1.1 404 Not Found
Date: Mon, 30 Apr 2018 07:41:41 GMT
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Content-Length: 316
Server: Jetty(9.3.6.v20151106)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 </title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /. Reason:
<pre>    Not Found</pre></p>
<hr /><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.6.v20151106</a><hr/>
</body>
</html>

如果服務器運行正常,但API路徑不正確:

$ curl -X POST -H "Content-Type: application/json" -D - -d '{}' http://localhost:8082/api/foobar
HTTP/1.1 404 Not Found
Date: Mon, 30 Apr 2018 07:42:57 GMT
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Content-Length: 326
Server: Jetty(9.3.6.v20151106)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 </title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /api/foobar. Reason:
<pre>    Not Found</pre></p>
<hr /><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.6.v20151106</a><hr/>

如果服務器運行正常,並且找到了API路徑:

$ curl -X POST -H "Content-Type: application/json" -D - -d '{}' http://localhost:8082/api/foobar
HTTP/1.1 200 OK
Date: Mon, 30 Apr 2018 07:50:30 GMT
Content-Type: text/plain;charset=iso-8859-1
Content-Length: 23
Server: Jetty(9.3.6.v20151106)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM