Я хочу добавить Employee на BamBhooHr, используя Bambhoohr REST API в MVC C #.
Я попытался опубликовать 2 типа контента, как показано в коде, но безуспешно.
1 ==>
public async System.Threading.Tasks.Task<JsonResult> AddEmployee(string fn,string ln)
{
var _resultModel = new BBHEmployee();
var _bambhoohrApi = "https://epicsoftsandbox.bamboohr.com/api/gateway.php/epicsoftsandbox/v1/employees";
var _apiKey = "b2aef724a48603468bfe85dce9e417ac8cf15fdf";
var _url = $"{_bambhoohrApi}";
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(_apiKey + ":x");
var base64encodedData = System.Convert.ToBase64String(plainTextBytes);
using (var _client = new HttpClient())
{
var _postData = new Dictionary<string, string>
{
{ "firstName", fn },
{ "lastName", ln }
};
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", base64encodedData);
var _response = await _client.PostAsync(_url, new FormUrlEncodedContent(_postData));
var _content = await _response.Content.ReadAsStringAsync();
_resultModel = JsonConvert.DeserializeObject<BBHEmployee>(_content);
}
return Json(_resultModel, JsonRequestBehavior.AllowGet);
}
2 ==>
public async System.Threading.Tasks.Task<JsonResult> AddEmployee(string fn,string ln)
{
var _resultModel = new BBHEmployee();
var _bambhoohrApi = "https://epicsoftsandbox.bamboohr.com/api/gateway.php/epicsoftsandbox/v1/employees";
var _apiKey = "b2aef724a48603468bfe85dce9e417ac8cf15fdf";
var _url = $"{_bambhoohrApi}";
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(_apiKey + ":x");
var base64encodedData = System.Convert.ToBase64String(plainTextBytes);
using (var _client = new HttpClient())
{
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", base64encodedData);
_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var jsonString = "{\"firstName\":\"" + fn + "\",\"lastName\":\"" + ln + "\"}";
StringContent _contt = new StringContent(jsonString, Encoding.UTF8, "application/json");
var _response = await _client.PostAsync(_url, _contt);
var _content = await _response.Content.ReadAsStringAsync();
_resultModel = JsonConvert.DeserializeObject<BBHEmployee>(_content);
}
return Json(_resultModel, JsonRequestBehavior.AllowGet);
}
в обе стороны возвращает StatusCode 400 (BadRequeest)