【问题标题】:getting "Response code 404 mapped as failure" by consuming web api from Mulesoft通过使用 Mulesoft 的 Web api 获取“响应代码 404 映射为失败”
【发布时间】:2017-11-09 21:44:48
【问题描述】:

我正在尝试使用开源 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:请求生成器

请帮我配置一下

【问题讨论】:

    标签: mule anypoint-studio mule-esb


    【解决方案1】:

    404 错误是由于您的 http:request 连接器构造了不正确的 url。

    来自 HTTP 请求连接器配置上的 MuleSoft documentation

    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

    【讨论】:

      【解决方案2】:

      尝试从 Postman/RestClient 中访问所需的 URL,看看是否会给出有效的响应。

      我已尝试使用发布的一些虚拟数据来复制您的流程。看来它成功了

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

      下面给出的响应

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

      如果它在邮递员中工作,它应该与骡子一起工作。如果它在邮递员而不是骡子中工作正常。用你的骡子流发布请求数据和网址(如果可能的话)。它将帮助我们找出根原因

      【讨论】:

        【解决方案3】:

        检查Webservice组件中的路径, 由于您已经指定了主机和端口,因此无需提供完整的路径。请更改路径并检查响应。

        【讨论】:

          【解决方案4】:

          完全同意 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>
          

          在路径中,只有 hte URL 的路径应该在主机名和端口之后。 由于您已经在连接器配置中定义了主机名和端口,因此路径中不应存在。

          【讨论】:

            【解决方案5】:

            检查您在同一端口上是否有另一台服务器(即使听起来太奇怪了)。

            当 Java Spring 服务器在 8082 端口和 MailSlurper 也在同一个端口 8082 时,我们遇到了同样的错误。使用 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 输出:

            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)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-09-11
              • 2017-08-20
              • 1970-01-01
              • 1970-01-01
              • 2014-04-01
              相关资源
              最近更新 更多