Fetching data from Google Translator API

401 Views Asked by At

I used HttpWebRequest in c#, I got message "502 Bad Gateway" that's my code, I think the problem is with the headers, But I Don't know What is it.

API link: https://rapidapi.com/googlecloud/api/google-translate1

string result = null;
string url = string.Format("https://google-translate1.p.rapidapi.com/language/translate/v2");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("x-rapidapi-host", "google-translate1.p.rapidapi.com");
request.Headers.Add("x-rapidapi-key", "7c92b1508emsh36b05b0798008p18a939jsn602418a477ab");

I use the same header in JavaScript and works fine.

1

There are 1 best solutions below

0
On

x-rapidapi-key and x-rapidapi-host are custom headers. In order to add custom headers, you need to pass the name of header as an index something like this:

request.Headers["x-rapidapi-key"] = "*****************";
request.Headers["x-rapidapi-host"] = "google-translate1.p.rapidapi.com";

P.S. You exposed your API key in the code snippet you posted above. It is not recommended due to security reasons. I recommend you to delete this API key and generate a new key from the RapidAPI Developer Dashboard