-
Jan 23rd, 2019, 08:33 AM
#1
Thread Starter
Fanatic Member
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
-
Jan 23rd, 2019, 10:42 AM
#2
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
-
Jan 23rd, 2019, 10:49 AM
#3
Thread Starter
Fanatic Member
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);
-
Jan 24th, 2019, 12:47 AM
#4
Thread Starter
Fanatic Member
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);
}
-
Jan 24th, 2019, 12:48 AM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|