简体   繁体   中英

How to send a GET request with body to an API Rest C#?

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM