Question Google Gemini AI

george_gr

New member
Joined
Feb 24, 2025
Messages
2
Programming Experience
5-10
Hello everybody, i have the c# code below to connect with Gemini AI and make requests with API (window application), the problem is the application is running perfect but only for me (windows 11), the other users get error 400 bad request. please could you help me to solve the problem? here is my code:

C#:
 string apiKey = "XXXXXXXXXXXXXXXXXXXXX";
        string prompt = "Search for: " + richTextBox1.Text;

       
        string jsonData = "{\"contents\":[{\"parts\":[{\"text\":\"" + prompt.Replace("\"", "\\\"") + "\"}]}]}";
        string url = "URL TO gemini-1.5-pro:generateContent?key=" + apiKey;

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/json";

        using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(jsonData);
            streamWriter.Flush();
        }

      
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                richTextBox2.Text = result;
            }
 
Last edited by a moderator:
Also, have you asked your users what they are entering into the richtext box? It looks like you are escaping only double quotes from the input, but not escaping backslashes and a few other special characters. You can check JSON to see what characters should be escaped.
 
May or may not be related to your problem. TLS 1.0 has been deprecated, but your line 8 is forcing the use of TLS 1.0.

Thank you, i changed to: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;

The program is still working fine for me, but other users still get 400 bad request. Does the API have some restrictions? i use the API for developers. This project was worked on the past in another one PC but now is working only on mine.
 
Is there more to the error message that Gemini sends back to those other users?

For example, if you look at this StackOverflow question, the error 400 is more specific about what was wrong with the request:
 
Back
Top Bottom