-->

对于微软Azure CORS支持翻译API?(CORS support for Microsoft

2019-09-29 23:53发布

这是涉及到一个问题https://social.msdn.microsoft.com/Forums/en-US/6e856136-9a39-4b98-a53d-7f8bce08e3a6/cors-support-for-bing-translate-api

请问微软Azure现在翻译API支持CORS?

Answer 1:

在微软翻译文本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&#xD;
Parameter name: appId</p><code></code><p>message id=0642.V2_Rest.Translate.4E779D02</p></body></html>

我个人不具有有效appid测试用,但如果这样做,我想你会发现它会工作:

  • 如果使用GET端点从https://docs.microsofttranslator.com/text-translate.html文档
  • 如果身份验证使用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我注意到文档说端点需要一个POSTapplication/xmltext/xml Content-Type ,因此,如果该端点没有按“T预检响应OPTIONS以正确的方式,一个是行不通的。

这是因为添加Content-Type: application/xmlContent-Type: text/xml头的请求一定会触发浏览器做预检OPTIONS的前POST



文章来源: CORS support for Microsoft Azure Translate API?