【发布时间】:2017-08-30 20:13:12
【问题描述】:
Microsoft Azure Translate API 现在是否支持 CORS?
【问题讨论】:
Microsoft Azure Translate API 现在是否支持 CORS?
【问题讨论】:
Microsoft Translator Text API 似乎至少现在支持 CORS,因为它至少似乎在响应中发送了 Access-Control-Allow-Origin 标头:
$ curl -i -H 'Origin: http://example.com' \
'https://api.microsofttranslator.com/v2/http.svc/Translate?appid=foo&text=hello&from=en&to=de'
HTTP/1.1 400 Bad Request
Content-Length: 220
Content-Type: text/html; charset=utf-8
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-MS-Trans-Info
X-MS-Trans-Info: 0642.V2_Rest.Translate.4E779D02
Date: Wed, 30 Aug 2017 09:07:34 GMT
<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: appId</p><p>Message: Invalid appId
Parameter name: appId</p><code></code><p>message id=0642.V2_Rest.Translate.4E779D02</p></body></html>
我个人没有有效的 appid 来测试,但如果你有,我想你会发现它会起作用:
GET 端点appid 参数而不是Authorization 请求标头进行身份验证如果可能也适用于https://docs.microsofttranslator.com/text-translate.html POST 端点——只要您的请求不使用Authorization 请求标头或设置Content-Type。
这些标头的问题是,它们会触发浏览器进行预检OPTIONS 请求:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
调用那些触发预检的 API 端点的问题是,它们似乎不会以一种会导致浏览器将预检视为成功的方式响应 OPTIONS 请求。
在https://docs.microsofttranslator.com/text-translate.html#!/default/post_TranslateArray 我注意到文档说端点需要POST 和application/xml 或text/xml Content-Type,所以如果该端点没有以正确的方式响应预检OPTIONS,那个不行。
这是因为在请求中添加 Content-Type: application/xml 或 Content-Type: text/xml 标头肯定会触发浏览器在 POST 之前执行预检 OPTIONS。
【讨论】:
Authorization。文档应该提到你刚才所说的一切