cURL : Request PUT

Hi everyone,

i have a problem, i use a function to send request to the API Twitch, with the method “GET” my function is ok but when i want to send a “PUT” request it’s not ok.

C# function :

    public T SendRequest<T>(string urlRequest, string p_method, string p_acces_token)
    {
        Thread.Sleep(100);
        JsonSerializer js = new JsonSerializer();
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlRequest);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "application/vnd.twitchtv.v3+json";
        httpWebRequest.Method = p_method;
        httpWebRequest.Headers.Add("Authorization: OAuth "+p_acces_token);
        if (p_method == "PUT")
        {
            httpWebRequest.ContentLength = urlRequest.Length;
            Stream response = httpWebRequest.GetRequestStream();
            using (StreamWriter writer = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                writer.Write(urlRequest);
                writer.Close();
            }
        }
        else
        {
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                T answer = JsonConvert.DeserializeObject<T>(streamReader.ReadToEnd());
                return answer;
            }
        }
        return default(T);
    }

Like i say, the else part of if is done. But the other one not, i think i need to send a request “write” ? i don’t know how it’s work.
Sorry for my verybad english.

Lol, 10sec after i find the solution :

     public T SendRequest<T>(string urlRequest, string p_method, string p_acces_token)
    {
        Thread.Sleep(100);
        JsonSerializer js = new JsonSerializer();
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlRequest);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "application/vnd.twitchtv.v3+json";
        httpWebRequest.Method = p_method;
        httpWebRequest.Headers.Add("Authorization: OAuth "+p_acces_token);
        if (p_method == "PUT")
        {
            httpWebRequest.ContentLength = 0;
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            T answer = JsonConvert.DeserializeObject<T>(streamReader.ReadToEnd());
            return answer;
        }
    }

just add to Content-Length : 0

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.