0

Hi I'm doing a windows forms project to interaction with an API. Until now i was able to develop the application, but now I've got a problem that I can't resolve. I'm trying to send a json body in a GET request like this:

Example :

NOTE: The example shows how to obtain the average value for the last three days of a temperature sensor. Request

GET http://api.effilogics.com/nodes/583/parameters/30/data?resolution=day&groupby=avg
{begin: '2014-04-21T16:13:30+02:00', end: '2014-04-24T15:44:12+02:00'}

I'm not sure if I'm sending the body (the date begin and end) correctly because I receive a message error that says that it's not possible to send the text content with this type of verbose. My question is how I can send the data/time body in a GET request?? I can't change to a POST request.

Part of the code is this:

public void Request4(String url)
    {
        try
        {                
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            myReq.Method = "GET";
            myReq.ContentType = "application/json";
            myReq.KeepAlive = true;
            myReq.Headers.Set("Cache-Control", "no-store");
            myReq.Headers.Set("Pragma", "no-cache");
            myReq.Headers.Set("Authorization", token_type + " " + access_token);


            datestruct dateST = new datestruct();
            dateST.begin = string.Concat("'begin':",DateTime.UtcNow.ToString("s",System.Globalization.CultureInfo.InvariantCulture),"+02:00");
            byte[] getBytes = Encoding.UTF8.GetBytes(dateST.begin);
            myReq.ContentLength = getBytes.Length;
            Stream body = myReq.GetRequestStream();
            body.Write(getBytes, 0, getBytes.Length);
            body.Close();

            //Obtenim la resposta del servidor
            WebResponse myResponse = myReq.GetResponse();
            Stream rebut = myResponse.GetResponseStream();
            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(rebut, Encoding.UTF8);
            //richTextBox6.AppendText(readStream.ReadToEnd() + "\n");
            string info = readStream.ReadToEnd();
            split3 = info.Split(new char[] { '[', '{', ',', '}', ']' });
            int mida = split3.Length;
            Array.Resize(ref split3, mida - 2);
            Array.Reverse(split3);
            mida = split3.Length;
            Array.Resize(ref split3, mida - 3);
            Array.Reverse(split3);
            foreach (string s in split3)
            {
                richTextBox6.AppendText(s);
                richTextBox4.AppendText(s);

            }
            myResponse.Close();
            readStream.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

If anyone can help me I will be so grateful. Thanks

kamesh
  • 2,374
  • 3
  • 25
  • 33
fondal
  • 45
  • 1
  • 2
  • 5
  • Please remove tag [tag:php] is not relevant to your question – Athafoud Jul 03 '14 at 12:16
  • First of all it not right to send something in body for get request. If datetime etc are parameters then add them to url itself. – loop Jul 03 '14 at 12:17
  • GET requests don't have a request body: http://stackoverflow.com/a/7274659/328193 – David Jul 03 '14 at 12:19
  • 2
    More correctly GET requests 'usually' have no message body... http://stackoverflow.com/questions/5216567/is-this-statement-correct-http-get-method-always-has-no-message-body but yes it is more usual to append parameters to the querystring for a GET request. – Paul Zahra Jul 03 '14 at 12:23

1 Answers1

1

As I explained in my comment to your question typically a GET request has no body, therefore I doubt the API will accept it (hence your error message), try encoding it into the url (just make sure your json object structure is the same as the parameters the API accepts)

Try building your url using something like Uri.EscapeUriString:

http://api.effilogics.com/nodes/583/parameters/30/data?resolution=day&groupby=avg + Uri.EscapeUriString(JSon)

P.S. SOAP UI (can be a bit hardcore) or Fiddler 2 (easy to use) are pretty good tools to use when working with REST.

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • FYI, fiddler will let you have message-body during GET and WebAPI gladly accepts it. Question is if your client can do it and if proxies (if applicable) will honor it. – G. Stoynev Mar 05 '15 at 17:12