开发者

Microsoft Translator API answers 500 internal server error

开发者 https://www.devze.com 2023-03-10 17:58 出处:网络
I\'m trying to use Microsoft\'s Translator API in my Rails app. Unfortunately and mostly unexpected, the server answers always with an internal server error. I also tried it manually with Poster[1] an

I'm trying to use Microsoft's Translator API in my Rails app. Unfortunately and mostly unexpected, the server answers always with an internal server error. I also tried it manually with Poster[1] and I get the same results.

In more detail, what am I doing? I'm creating an XML string which goes into the body of the request. I used the C# Example of the API documentation. Well, and then I'm just invoking the RESTservice. My code looks like this:

xmlns1 = "http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"
xmlns2 = "http://schemas.microsoft.com/2003/10/Serialization/Arrays"

xml_builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
  xml.TranslateArrayRequest("xmlns:ms" => xmlns1, "xmlns:arr" => xmlns2) {
     xml.AppId token  #using temporary token instead of appId
     xml.From  source
     xml.To    target

     xml.Options {
       xml["ms"].ContentType {
         xml.text "text/html"
       }
     }

     xml.Texts {
       translate.each do |key,val|
          xml["arr"].string {
            xml.text CGI::unescape(val)
          }
       end
     }
   }
end

headers = {
   'Content-Type' => 'text/xml'
}

uri = URI.parse(@@msTranslatorBase + "/TranslateArray" + "?appId=" + token) 
req = Net::HTTP::Post.new(uri.path, headers)
req.body = xml_builder.to_xml

response = Net::HTTP.start(uri.host, uri.port) { |http| http.request(req) }
# [...]

The xml_builder produces something like the following XML. Differently to the example from the API page, I'm defining two namespaces instead of referencing them on the certain tags (mainly because I wanted to reduces the overhead) -- but this doesn't seem to be a problem, when I do it like the docu-example I also get an internal server error.

<?xml version="1.0" encoding="UTF-8"?>
<TranslateArrayRequest xmlns:ms="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <AppId>TX83NVx0MmIxxCzHjPwo2_HgYN7lmWIBqyjruYm7YzCpwnkZL5wtS5oucxqlEFKw9</AppId>
  <From>de</From>
  <To>en</To>
  <Options>
    <ms:ContentType>text/html</ms:ContentType>
  </Options>
  <Texts>
    <arr:string>Bitte übersetze diesen Text.</arr:string>
    <arr:string>Das hier muss auch noch übersetzt werden.</arr:string>
  </Texts>
</TranslateArrayRequest>

Every time I request the service it answers with

#<Net::HTTPInternalServerError 500 The server encountered an error processing the request. Please see the server logs for more details.>

... except I do some unspecified things, like using GET instead of POST, then it answers with something like "method not allowed".

I thought it might be something wrong with the XML stuff, because I can request an AppIdToken and invoke the 开发者_运维问答Translate method without problems. But to me, the XML looks just fine. The documentation states that there is a schema for the expected XML:

The request body is a xml string generated according to the schema specified at http:// api.microsofttranslator.com/v2/Http.svc/help

Unfortunately, I cannot find anything on that.

So now my question(s): Am I doing something wrong? Maybe someone experienced similar situations and can report on solutions or work-arounds?


[1] Poster FF plugin > addons.mozilla.org/en-US/firefox/addon/poster/


Well, after lot's of trial-and-error I think I made it. So in case someone has similar problems, here is how I fixed this:

Apparently, the API is kind of fussy with the incoming XML. But since there is no schema (or at least I couldn't find the one specified in the documentation) it's kind of hard to do it the right way: the ordering of the tags is crucial!

<TranslateArrayRequest>
  <AppId/>
  <From/>
  <Options />
  <Texts/>
  <To/>
</TranslateArrayRequest>

When the XML has this ordering it works. Otherwise you'll only see the useless internal server error response. Furthermore, I read a couple of times that the API also breaks if the XML contains improper UTF-8. One can force untrusted UTF-8 (e.g. coming from a user form) this way:

ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
valid_string = ic.iconv(untrusted_string + ' ')[0..-2]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号