I am making a post
call to my .net core api from client using httpclient
class, my api hosted on Azure.
When payload passed to api comntroler method with simple data without any special charcters call get succesfully executed, but when call made with special charcters like "%`` it get reject with status code forbidden
by api gateways. We cannot change Azure api gatway policy, so some how I need to change my post data content to send special charcters in post method.
Client Call Sample
using System;
class Program
{
static void Main(string[] args)
{
Task.Run(() => MainAsync());
Console.ReadLine();
}
static async Task MainAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://postCallPoc.net");
var emp = new Employee()
{
Name = "Jhon",
Address = "%House no 20, near Clutch & Gair showroom Mumbai",
DOJ = DateTime.Now.Date,
Note = "%Verificaion Pending"
};
var httpContent = GetSerializeContent(emp);
var result = await client.PostAsync("/api/Employee/add", httpContent);
Console.WriteLine(result.StatusCode); //receiving the responce as Forbidden
}
}
public class Employee
{
public int? ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public DateTime DOJ { get; set; }
public string Note { get; set; }
}
}
HTPP Content Creator
private StringContent GetSerializeContent(dynamic content)
{
var serilizedContent = JsonConvert.SerializeObject(content);
//var buffer = System.Text.Encoding.UTF8.GetBytes(serilizedContent);
var result = new StringContent(serilizedContent, System.Text.Encoding.UTF8);
result.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return result;
}
API Controler
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using DA.Common.Master;
using DataEntity;
using ApiCommon;
using Common;
namespace MasterData.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
[Consumes("application/json")]
[Produces("application/json")]
public class EmployeeController : ControllerBase
{
ILogger<EmployeeController> _logger;
public EmployeeController( ILogger<EmployeeController> logger)
{
_logger = logger;
}
[HttpPost]
public ActionResult<ApiResponse> Add(Employee emp)
{
ApiResponse response;
try
{
var dbResponce = EmpoyeeBal.Insatnce.Save(emp);
response = new ApiResponse
{
Data = dbResponce
};
return Ok(response);
}
catch (Exception ex)
{
_logger.Log(ex);
}
}
}
}
ERROR
403 Forbidden403 Forbidden
Microsoft-Azure-Application-Gateway/v2