Capture Terminal Output

Started by nrr, December 15, 2023, 10:29:34 AM

Previous topic - Next topic

td

Windows can be a bit buggy at times. COM Automation works in UTF-16 LE so the COM object internally translates the UTF-8 to Unicode before returning the response in the call to the "responseText" object method. It would appear to be using the current user or system default code page for the translation instead of using UTF-8. That would be an MSFT bug. I did get the following to work, however:

Code (winbatch) Select
objHttp=ObjectCreate("Msxml2.XMLHTTP.6.0") ;WinHttp.WinHttpRequest.5.1")

URL = "https://api.deepl.com/v2/translate"
objHttp.open("POST",URL,"False")
objHttp.SetRequestHeader("Authorization", "DeepL-Auth-Key xxxxxxx-xxxx-xxxx-xxxxxxxx" )   ; Put your key here!
objHttp.SetRequestHeader("User-Agent", "MyTranslate/1.1.1") ; Put your "app" name here!
objHttp.SetRequestHeader("Content-Length", "71")
objHttp.SetRequestHeader("Accept", "application/json")
objHttp.SetRequestHeader("Content-Type", "application/json")   
Body = `{"text":["The table is green. The chair is black."],"target_lang":"DE"}`
objHttp.Send(body)
while objHttp.readyState != 4
   objHttp.WaitForResponse(1000)
endwhile
Terminate(objHttp.Status != 200,"DeepL Translator", "Translation request failed")

strResponse=objHttp.responseText
objHttp=0
;FilePutW("C:\temp\strResponse.txt", strResponse)
exit


If this doesn't work for you, it would be great if you could provide the text causing the problem.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Worked for me... a little parsing logic needed for response

nrr

It works!  Thank you so much.   See link to picture of my app ...

https://app.screencast.com/JbyLXTwZOmeVn

kdmoyers

The mind is everything; What you think, you become.

td

Quote from: nrr on December 27, 2023, 05:38:55 PM
It works!  Thank you so much.   See link to picture of my app ...

https://app.screencast.com/JbyLXTwZOmeVn

According to MSFT docs the WaitForResponse method's parameter is in second. I had mistakenly remembered it as being in milliseconds. Assuming you included the line in your script, you might want to consider changing the parameter value for the method call.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade