2

I need to have body element (JSON) with RESTSharp GET call, Although this sounds not correct, I have no other option since third party REST service provider wants me to send GET request but it needs certain parameters in the body,

When I try to user AddBody in RESTSharp request with GET method, it doesn't seem to include the body in final request (when checking with Fiddler)

Is there a way to create GET Request with a JSON body?

Tekz
  • 1,279
  • 14
  • 20
  • Nope. Would using QueryString parameters be an option? – cecilphillip Jan 28 '13 at 18:52
  • Yes that's what I wanted to use, but unfortunately this third party service requires certain parameters in its body. I'm not sure why they have done it, and I'm trying to get answer from them as well. There are certain DELETE methods which requires parameters in body as well. I'm not sure why they have done it in such way – Tekz Jan 29 '13 at 21:07
  • I'd definitely say contact them for some clarification / sample requests – cecilphillip Jan 29 '13 at 21:14

2 Answers2

1

I have the same issue. I first tried to change Restsharp source code so that in a GET request it would try to write body part. But then I got exception "cannot send a content body with this verb type", did another search:

https://stackoverflow.com/a/3981584/2291894

It seems this cannot be realized in .net

Community
  • 1
  • 1
leo1987
  • 131
  • 1
  • 6
0

I don't know how to do it in RestSharp but I was able to get it working using this alternative approach.

    string GetCustomerId(string distributorId, string email, string userName, KBToken token)
    {
        var req = WebRequest.Create("https://testapi/rest/v1/customers");

        req.ContentType = "application/json";
        req.Method = "GET";
        req.Headers.Add("Authorization", "Bearer " + token.access_token);

        var type = req.GetType();
        var currentMethod = type.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(req);

        var methodType = currentMethod.GetType();
        methodType.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentMethod, false);

        using (var streamWriter = new StreamWriter(req.GetRequestStream()))
        {
            streamWriter.Write(JsonConvert.SerializeObject(new
            {
                distributorId = distributorId,
                email = email,                    
                username = userName
            }));
        }            

        //getting response (irrelevant here)
        var customerId = "";
        var response = req.GetResponse();
        var responseStream = response.GetResponseStream();
        if (responseStream != null)
        {
                           var myStreamReader = new StreamReader(responseStream, Encoding.Default);
            var resultEntity = myStreamReader.ReadToEnd();

            JObject jObjectItems = JObject.Parse(resultEntity);
            var items = jObjectItems.SelectToken("items");

            if (items.Count() > 0)
            {
                JObject jObject = JObject.Parse(JArray.Parse(items.ToString())[0].ToString());
                customerId = jObject["id"].Value<int>().ToString();
            } 
        }
        responseStream.Close();
        response.Close();
        return customerId;
    }
estinamir
  • 435
  • 5
  • 11