Hi I am unbale to call custom translator API getting following error [code and error attachecd]

%3CLINGO-SUB%20id%3D%22lingo-sub-2172075%22%20slang%3D%22en-US%22%3EHi%20I%20am%20unbale%20to%20call%20custom%20translator%20API%20getting%20following%20error%20%5Bcode%20and%20error%20attachecd%5D%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2172075%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EPlease%20Help%20me%20with%20python%20script%20for%20calling%20API%20services%20thank%20you%3C%2FSTRONG%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Screenshot%202021-02-28%20at%203.32.05%20PM.png%22%20style%3D%22width%3A%20902px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F258708i2B5370A6C4974707%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Screenshot%202021-02-28%20at%203.32.05%20PM.png%22%20alt%3D%22code%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Ecode%3C%2FSPAN%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Screenshot%202021-02-28%20at%203.29.47%20PM.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F258709i1900647DECD53671%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Screenshot%202021-02-28%20at%203.29.47%20PM.png%22%20alt%3D%22error%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Eerror%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

Please Help me with python script for calling API services thank youcodecodeerrorerror

1 Reply

@vardhaman1470 

 

Welcome to the Microsoft Tech Community, I assume there is a reason your using the http.client library rather than the 'requests' library which would make this a whole lot easier for yourself. 

 

Anyway the error your getting is because your mis constructing the python request url. In python 3 when you create the http.client object with

conn = http.client.HTTPSConnection("api.cognative.microsofttranslator.com")

you do not then need to construct the full url when you make the "POST" call.

 

I.e. you have:

conn.request("POST", constructed_url + "&category=" +cat, content, headers)
#where constructed url = "https://api.cognative.microsofttranslator.com/translate?api-version=3.0&to=en"
# Note: for some reason your also setting the variable 'path' twice.
#
#
# This effectively means your making a call that looks like this: http://api.cognative.microsofttranslator.com/https://api.cognative.microsofttranslator.com/translate?api-version=3.0&to=en"
# Hence the reply your getting from the server is that directory doesnt exist formatted in HTML

 

To make this work you should remove base_url from the constructed_url variable. i.e.

#change
consturcted_url = base_url + path + ToLanguage

#to

constructed_url = path + ToLanguage

 

I think after that your call will work.