Feb 28 2021 02:03 AM - edited Feb 28 2021 02:05 AM
Please Help me with python script for calling API services thank you
Mar 01 2021 07:41 AM
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.