Results 1 to 5 of 5

Thread: How to HTTP GET Request with a Body?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    782

    How to HTTP GET Request with a Body?

    Dear All,

    How to HTTP GET Request with a Body?
    some of our B2B Web API requires GET request but with a Body..

    any body please help?

    thanks a lot in advance

    Regards
    Win

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: How to HTTP GET Request with a Body?

    That doesn't make a lot of sense, why would you add a body element in a Get Request ?

    Also if your sure you need this can you post one of your Get web methods that require a body so we can see it ? it might help us understand exactly why its required
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    782

    Re: How to HTTP GET Request with a Body?

    I know this is not make sense, but our B2B party requires it..
    I tried to use codes below.. but not working


    Code:
     List<String> _calcs = new List<String>();
                _calcs.Add("fill_count");
    
                filter f = new filter();
                f.relation = "equals";
                f.attribute = "state";
                f.value = "CA";
    
                insights i = new insights();
                i.field = "family.behaviors";
                i.calculations = _calcs.ToArray();
    
                Test t = new Test();
                t.filter = f;
                t.insights = i;
    
                JObject j = JObject.FromObject(t);
                string _j = j.ToString();
    
                var client = new RestClient("example.com/path1/test");
                var request = new RestRequest(Method.GET);
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("API-TOKEN", _apiKey);
    
                request.AddJsonBody(_j);
    
                IRestResponse response = client.Get(request);

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    782

    Re: How to HTTP GET Request with a Body?

    finally, I got it!...

    Netframework does not support this capability.. but Net Core does! .. (tested on Net Core 2.2)

    Code:
            public async void Post([FromBody] string value)
            {
                List<String> _calcs = new List<String>();
                _calcs.Add("fill_count");
    
                filter f = new filter();
                f.relation = "equals";
                f.attribute = "state";
                f.value = "CA";
    
                insights i = new insights();
                i.field = "family.behaviors";
                i.calculations = _calcs.ToArray();
    
                Test t = new Test();
                t.filter = f;
                t.insights = i;
    
                JObject j = JObject.FromObject(t);
                string _j = j.ToString();
    
                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage
                    {
                        RequestUri = new Uri("https://example/path1/test"),
                        Method = HttpMethod.Get,
                    };
    
                    request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(_j));
    
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    request.Content.Headers.Add("X-AUTH-TOKEN", _apiKey);
                    var result = client.SendAsync(request).Result;
                    result.EnsureSuccessStatusCode();
    
                    var responseBody = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
                }

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    782

    Resolved Re: How to HTTP GET Request with a Body?

    finally, I got it!...

    Netframework does not support this capability.. but Net Core does! .. (tested on Net Core 2.2)

    Code:
            public async void Post([FromBody] string value)
            {
                List<String> _calcs = new List<String>();
                _calcs.Add("fill_count");
    
                filter f = new filter();
                f.relation = "equals";
                f.attribute = "state";
                f.value = "CA";
    
                insights i = new insights();
                i.field = "family.behaviors";
                i.calculations = _calcs.ToArray();
    
                Test t = new Test();
                t.filter = f;
                t.insights = i;
    
                JObject j = JObject.FromObject(t);
                string _j = j.ToString();
    
                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage
                    {
                        RequestUri = new Uri("https://example/path1/test"),
                        Method = HttpMethod.Get,
                    };
    
                    request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(_j));
    
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    request.Content.Headers.Add("X-AUTH-TOKEN", _apiKey);
                    var result = client.SendAsync(request).Result;
                    result.EnsureSuccessStatusCode();
    
                    var responseBody = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
                }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width