2016-11-18 13 views
0

私は、HTTP POST要求を受け取り、角度で記述されたUIアプリケーションから呼び出されるmule 3.7.3ワークフローを持っています。HTTP POSTメソッドが返されます。メソッドがOPTIONSに設定されているため、Muleでメソッドが許可されていません。

http POSTコールがMuleに送信されると、httpメソッドがOPTIONSに設定された状態でメッセージが送信されているため、メソッドが許可されないため失敗します。これは、content typeがapplication/jsonに設定されたXHRタイプのメッセージを送信するアプリケーションでは予期された動作であることがわかりました。このような状況では、UIアプリケーションはPOST要求を送信する前にOPTIONS要求をプリフライトします。

私の質問は以下のとおりです。

  1. UIだけPOSTリクエストを送信することが可能それ?
  2. ミュールがこれを処理する最良の方法は何ですか?すべてのPOSTメソッドと一緒にRAMLファイルにOPTIONS呼び出し を追加し、さらに ワークフローを作成する必要がありますか?

XMLフロー:CORSの問題だ

<?xml version="1.0" encoding="UTF-8"?> 
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:cors="http://www.mulesoft.org/schema/mule/cors" xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/cors http://www.mulesoft.org/schema/mule/cors/current/mule-cors.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/apikit http://www.mulesoft.org/schema/mule/apikit/current/mule-apikit.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd"> 
    <apikit:config name="test-order-config" raml="test-order.raml" consoleEnabled="true" consolePath="console" doc:name="Router"> 
     <apikit:flow-mapping resource="/orders/order" action="post" content-type="application/json" flow-ref="post:/orders/order:test-order-config"/>  
    </apikit:config> 

    <cors:config name="Cors_Configuration" doc:name="Cors Configuration"> 
     <cors:origins> 
      <cors:origin url="*"> 
       <cors:methods> 
        <cors:method>POST</cors:method> 
        <cors:method>DELETE</cors:method> 
        <cors:method>PUT</cors:method> 
        <cors:method>GET</cors:method> 
       </cors:methods> 
       <cors:headers> 
        <cors:header>content-type</cors:header> 
       </cors:headers> 
      </cors:origin> 
     </cors:origins> 
    </cors:config> 

    <flow name="test-order-main"> 
     <http:listener config-ref="HTTP_Listener_Configuration" path="/*" doc:name="HTTP"/> 
     <cors:validate config-ref="Cors_Configuration" publicResource="true" acceptsCredentials="false" doc:name="CORS Validate"/> 
     <apikit:router config-ref="test-order-config" doc:name="APIkit Router"/>    
     <exception-strategy ref="test-order-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/> 
    </flow> 

    <flow name="set-access-control-allow-values"> 
     <set-property propertyName="Access-Control-Allow-Origin" value="*" doc:name="Set Access Control Allow Origin"/> 
     <set-property propertyName="Access-Control-Allow-Credentials" value="false" doc:name="Set Access Control Allow Credentials"/> 
     <set-property propertyName="Access-Control-Allow-Methods" value="GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS" doc:name="Set Access Control Allow Methods"/> 
     <set-property propertyName="Access-Control-Allow-Headers" value="DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,origin,authorization,accept,client-security-token" doc:name="Set Access Control Allow Headers"/> 
    </flow> 

    <flow name="get:/products:test-order-config"> 
     <set-payload value="#['{ &quot;getProducts&quot;: { &quot;productsList&quot;: [{ &quot;productId&quot;: &quot;001&quot;, &quot;name&quot;: &quot;test_product1&quot;}]}}']" doc:name="Set Payload"/> 
     <flow-ref name="set-access-control-allow" doc:name="set-access-control-allow"/> 
     <logger message="#[payload]" level="INFO" doc:name="Logger"/> 
    </flow> 

    <flow name="get:/customer/{customerId}:test-order-config"> 
     <set-payload value="#['{&quot;getCustomer&quot;:{&quot;customerId&quot;:&quot;1234567890&quot;,&quot;title&quot;:&quot;MR&quot;,&quot;Name&quot;:&quot;John&quot;}}']" doc:name="Set Payload"/> 
     <flow-ref name="set-access-control-allow" doc:name="set-access-control-allow"/> 
     <logger message="#[payload]" level="INFO" doc:name="Logger"/> 
    </flow> 

    <flow name="post:/orders/order:test-order-config"> 
     <set-payload value="#['{&quot;createOrder&quot;:{&quot;orderId&quot;:&quot;8a493ecd-e842-4ca2-b33b-a03aa9136673&quot;,&quot;success&quot;:true}}']" doc:name="Set Payload"/> 
     <flow-ref name="set-access-control-allow" doc:name="set-access-control-allow"/> 
     <logger message="#[payload]" level="INFO" doc:name="Logger"/> 
    </flow> 

    <apikit:mapping-exception-strategy name="test-order-apiKitGlobalExceptionMapping"> 
     <apikit:mapping statusCode="404"> 
      <apikit:exception value="org.mule.module.apikit.exception.NotFoundException"/> 
      <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/> 
      <set-payload value="{ &quot;message&quot;: &quot;Resource not found&quot; }" doc:name="Set Payload"/> 
     </apikit:mapping> 
     <apikit:mapping statusCode="405"> 
      <apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException"/> 
      <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/> 
      <set-payload value="{ &quot;message&quot;: &quot;Method not allowed&quot; }" doc:name="Set Payload"/> 
     </apikit:mapping> 
     <apikit:mapping statusCode="415"> 
      <apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException"/> 
      <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/> 
      <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/> 
     </apikit:mapping> 
     <apikit:mapping statusCode="406"> 
      <apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException"/> 
      <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/> 
      <set-payload value="{ &quot;message&quot;: &quot;Not acceptable&quot; }" doc:name="Set Payload"/> 
     </apikit:mapping> 
     <apikit:mapping statusCode="400"> 
      <apikit:exception value="org.mule.module.apikit.exception.BadRequestException"/> 
      <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/> 
      <set-payload value="{ &quot;message&quot;: &quot;Bad request&quot; }" doc:name="Set Payload"/> 
     </apikit:mapping> 
    </apikit:mapping-exception-strategy> 

</mule> 

おかげ

+2

私はラバを知らないが、あなたはそれがOPTIONSを受け入れるようにする必要があります。要求は結果を返す必要はありませんが、200ステータスで応答する必要があります。 –

+1

おそらくあなたは 'CORS'のためにそれに直面しているでしょう。あなたは実際のエラーストリングを投稿できますか? –

答えて

0

、OPTIONS要求は、プリフライトリクエストです。

OPTIONSを許可するか、それは公共のすべてを受け入れるために作るためにCORSモジュールを使用します。

<cors:config name="corsConfig" /> 

<flow name="rest-api-main" doc:name="rest-api-main"> 
     <http:listener path="api/${api.version}/*" config-ref="restAPIMainHttpConfig" /> 

     <cors:validate config-ref="corsConfig" publicResource="true" /> 
</flow> 

更新

xmlns:core="http://www.mulesoft.org/schema/mule/core" 

http://www.mulesoft.org/schema/mule/cors http://www.mulesoft.org/schema/mule/cors/current/mule-cors.xsd 

をしかし、あなたはパレットからドラッグした場合スタジオでは自動的に追加されます。

そしてHERESにMavenの依存関係:

<dependency> 
     <groupId>org.mule.modules</groupId> 
     <artifactId>mule-module-cors</artifactId> 
     <version>2.1.1</version> 
    </dependency> 
+0

ありがとうございます。どんな宣言も必要ですか?それは失敗している要素 "cors:config"のプレフィックス "cors"はバインドされていません – user3165854

+0

は情報で私の答えを更新しました。 –

+0

こんにちは。私はワークフローが正しく起動されていないようにCORSをテストするために私が作業している例を追加しました。私が流れから逃しているものを確かめないでください。 – user3165854

関連する問題