ThingWorx Flow > ThingWorx Flow 安裝與管理 > 管理 ThingWorx Flow > 對 ThingWorx Flow 執行外部呼叫
對 ThingWorx Flow 執行外部呼叫
可使用下列其中一種方法來對 ThingWorx Flow 進行外部呼叫:
從外部網站對 ThingWorx Flow 進行 REST 呼叫
如果某個網站嘗試對 ThingWorx Flow 伺服器進行 REST 呼叫,則此伺服器會阻止此請求,因為它是一個跨原始來源請求。若要允許此請求,必須將 ThingWorx Flow 伺服器配置為允許來自網站的跨原始來源請求。將 CORS (跨原始來源資源共用) 篩選器新增至 ThingWorx Flow 伺服器,可啟用跨原始來源請求。然後,在其他伺服器上部署的其他網站便可存取 ThingWorx Flow 伺服器中的資料。
完成下列步驟,可讓網站對 ThingWorx Flow 進行 REST 呼叫:
1. 請關閉任何執行中的 Nginx 實例。
2. 在 Nginx 安裝下的下列位置開啟 vhost-flow.conf 檔案。
Windows: C:/Program Files/nginx-<version>/conf/conf.d
Linux: /etc/nginx/conf/conf.d
3. vhost-flow.conf 檔案的 location /Thingworx 部份下,找到 proxy_set_header X-Content-Type-Options nosniff; 一行,並新增下列幾行:
set $cors '';
# Right side of condition can be regular expression:
# if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)')
if ($http_origin = '<Origin_that_calls_ThingWorx_Flow>') {
set $cors 'true';
}
if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
}
# OPTIONS indicates a CORS pre-flight request
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '$http_origin';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
4. 如果您已在不同的電腦上安裝 ThingWorx FoundationThingWorx Flow,請執行下列操作:
vhost-flow.conf 檔案中找到下面這一行:
# add_header Content-Security-Policy
在上述一行下面新增下列行:
add_header Content-Security-Policy "frame-ancestors 'self' <Origin_that_calls_ThingWorx_Flow>;";
您可以對功能變數名稱進行硬式編碼或提供 RegEx。例如:add_header Content-Security-Policy "frame-ancestors 'self' https://*.ptcnet.ptc.com;";
5. <Origin_that_calls_ThingWorx_Flow> 取代為嘗試存取 ThingWorx Flow 之網站的 URL。
6. 重新啟動 Nginx 服務。
對 ThingWorx Flow 執行 WebSocket 呼叫
依預設,您可對 ThingWorx Flow 進行 WebSocket Secure (wss) 呼叫。
不支援 WebSocket (ws) 呼叫。