Search code examples
c#webclientgoogle-translate

How do I send a POST Request to Google Translate Api using a C# WebClient?


I googled now for almost an hour and can't find anything that would help me out. I'm a beginner programer and took the course over from TeamTreeHouse with Serialization in C#. Here I also learned how to use the WebClient.

Now I thought a good practice Project would be to make a Translate app that sends to google the user input in a POST Request and Google returns the answer as Json which I deserialize.

Problem is I read through the documentation of the api but I'm so confused of what I should send exactly to google and really how to do this?

I know the method webclient.Headers.Add(arguments here) , but I really don't know what else it needs.

You can find an example over here:

https://cloud.google.com/translate/docs/translating-text

https://translation.googleapis.com/language/translate/v2 Three query parameters are required with each translation request:

Target language: Use the target parameter to specify the language you want to translate into. Source text string: Use the q parameter to specify each text string to translate. API key: Use the key parameter to identify your application. If you are using OAuth 2.0 service account credentials (recommended), do not supply this parameter.

So problem is there is no placeholder in that example URL where I could put my api key + soure text string an Target language.

So what exactly should I send to google so it knows what I want and returns me the JSON file?

Maybe anyone could help me out. And I know there is an official Library for exactly this but I want to practice serialization and using web scraping with the WebClient class so I want to do it like this.


Solution

  • The documentation here: https://cloud.google.com/translate/docs/reference/translate#body.QUERY_PARAMETERS

    says use query parameters q, target, key, etc. So your URL should be something like https://translation.googleapis.com/language/translate/v2?key=[yourAPIkey]&target=language