Doubt with WebRequest method get passing token

1

I'm trying to read the data of an application, where I send the information via get, Error: Can not send content with this type of verb.

     public string ConsultaPedido(string urlpedido, string NumeroPedido)
        {
            var request = (HttpWebRequest)WebRequest.Create(urlpedido + "/" + NumeroPedido +"/");
            request.ContentType = "application/json";
            request.Method = "GET";

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    AuthToken = "8686330657058660259"
                });

                streamWriter.Write(json);
            }

            var response = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                return streamReader.ReadToEnd();
            }
        }

    
asked by anonymous 06.12.2017 / 02:14

2 answers

1

GET method does not contain Body , meaning you need to send AuthToken to Header as our colleague said. Try this:

public string ConsultaPedido(string urlpedido, string NumeroPedido)
    {
        var request = (HttpWebRequest)WebRequest.Create(urlpedido + "/" + NumeroPedido +"/");
        //request.ContentType = "application/json"; removendo o body
        request.Method = "GET";

        /*using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(new
            {
                AuthToken = "8686330657058660259"
            });

            streamWriter.Write(json);
        }Não há necessidade de adicionar um body, ao invés disso adicione no Header.*/

        request.Headers["AuthToken"] = "8686330657058660259";//Adicionando o AuthToken  no Header da requisição

        var response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            return streamReader.ReadToEnd();
        }
    }

I removed the line from adding the Header of this question in English

    
06.12.2017 / 14:47
0

You're trying to send a Body into a GET verb where it's not possible. What you can do and is most common is you send the token in the Header.

Example:

Iusuallydothis,anexamplemethodofatestthatIdid:

privateList<UserCoupon>GetUserCoupons(){varclient=newHttpClient{BaseAddress=newUri(APIUrl)};if(Session["User"] != null)
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetToken());

        int userId = GetUserId();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        var response = client.GetAsync($"api/coupon/couponlistbyuser?UserId={userId}").Result;

        if (response.IsSuccessStatusCode)
        {
            var ret = response.Content.ReadAsStringAsync();
            var list = JsonConvert.DeserializeObject<List<UserCoupon>>(ret.Result);
            return list;
        }
    }
    return null;
}
    
06.12.2017 / 09:11