Once I'm trying to call web API controller method I'm getting this error,
Cannot send a content-body with this verb-type.
My Details controller as follows,
[HttpGet("requestDetails/Details")]
public IActionResult GetDetails([FromHeader] int tenantId,[FromBody]ParamterDTO pdto)
{
try
{
var details = Service.GetDetials(pdto.FromDate, pdto.SearchText, pdto.UserId);
return Ok(details);
}
catch (Exception)
{
return StatusCode(500);
}
}
This how I consume Details controller method.
public string GetDetails(string fromDate, string searchText, string userID) {
try {
string serviceUrl = "http://localhost/Testapi/api/details/requestDetails/Details";
string jsonParamterData = new JavaScriptSerializer().Serialize(new {
FromDate = fromDate,
SearchText = searchText,
UserId = userID
});
HttpClient client = new HttpClient();
HttpMethod method = new HttpMethod("GET");
HttpRequestMessage message = new HttpRequestMessage(method, serviceUrl);
StringContent content = new StringContent(jsonParamterData, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("TenantId", tenantId.ToString());
client.DefaultRequestHeaders.Add("Authorization", string.Format("bearer {0}", token));
message.Content = content;
var response = client.SendAsync(message).Result;
client.Dispose();
return response.Content.ToString();
} catch (Exception ex) {
NameValueCollection logParams = new NameValueCollection();
Logger.LogErrorEvent(ex, logParams);
throw;
}
}
Can anyone explain me, what did I do wrong here.
Updated:
I used post instead of get by changing code as,
HttpMethod method = new HttpMethod("POST");
HttpRequestMessage message = new HttpRequestMessage(method, serviceUrl);
StringContent content = new StringContent(jsonParamterData, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("TenantId", tenantId.ToString());
client.DefaultRequestHeaders.Add("Authorization", string.Format("bearer {0}", token));
message.Content = content;
//var response = client.GetStringAsync(message.).Result;
var response = client.SendAsync(message);
but response result showing as follows,
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…