I want to add Employee on BamBhooHr using Bambhoohr REST API in MVC C#.
I have tried 2 content types to post as shown in the code but not succeed.
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);
}
with both ways, it returns StatusCode 400(BadRequeest)
in that case, this BambhooHr API endpoint requires XML data to post,
so I use this and it's working fine.
var xmlString = "<employee><field id = \"firstName\">"+fn+"</field><field id = \"lastName\">"+ln+"</field></employee>";
var _contt = new StringContent(xmlString,Encoding.UTF8, "text/xml");
var _response = await _client.PostAsync(_url, _contt);
Related
I have the following GraphQl query, which works fine in PostMan.
query getFirstName($id: String!) {
getFirstName(id: $id) {
myId
}
}
However, when I am executing using the traditional HttpClient module I get the error
code":400,"stacktrace":"Syntax Error: Expected Name, found \"$
My Code:
var queryObject = new
{
query = #"query {
getFirstName($id: String!) {
getFirstName(id: $id) {
myId
}
}
}",
variables = new { id = "132" }
};
httpClient.DefaultRequestHeaders.Accept.Clear();
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
httpClient.DefaultRequestHeaders.Accept.Add(contentType);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var myContent = JsonConvert.SerializeObject(queryObject);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var responseTask = httpClient.PostAsync(myUri, byteContent);
responseTask.Wait();
var response = responseTask.Result;
I think my queryObject is not parsed correctly.
The Json string has this structure:
{"CODIGO_AGENCIA":"HN001001","CODIGO_USUARIO":"some user","CODIGO_CATEGORIA":1}
This is the parameter asked by the WS:
public async Task SubirImagenCategoria(string JsonString, HttpPostedFileBase Archivo)
//This is what I got so far, the web service returns error that the json string is empty, I am completely lost on how to proceed.
public static async Task<CustomJsonResult> SubirImagenCategoría(int CodigoCategoria, HttpPostedFileBase Archivo)
{
usuario = UtilClass.GetUsuarioSesion();
var modelo = new SUBIR_IMAGEN_CAT();
modelo.CODIGO_AGENCIA = usuario.CodigoAgencia;
modelo.CODIGO_USUARIO = usuario.Nombre;
modelo.CODIGO_CATEGORIA = 1;
CustomJsonResult result = new CustomJsonResult();
try
{
var JsonString = JsonConvert.SerializeObject(modelo);
var formContent = new MultipartFormDataContent("form-data");
StringContent jsonPart = new StringContent(JsonString.ToString());
jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");
formContent.Add(jsonPart);
/* byte[] Bytes = new byte[Archivo.InputStream.Length + 1];
Archivo.InputStream.Read(Bytes, 0, Bytes.Length);
var fileContent = new ByteArrayContent(Bytes);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { FileName = Archivo.FileName };
formContent.Add(fileContent);*/
StreamContent filePart = new StreamContent(Archivo.InputStream);
filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
filePart.Headers.ContentDisposition.FileName = Archivo.FileName;
formContent.Add(filePart);
var test = formContent;
/*HttpContent jsonParam = new StringContent(JsonString);
HttpContent fileStream = new StreamContent(Archivo.InputStream);
formData.Add(jsonParam, "JsonString", "JsonString");
formData.Add(fileStream, "Archivo", "Archivo");*/
/*var values = new Dictionary<string, string>
{
{ "JsonString", ("{\"CODIGO_AGENCIA\":"+usuario.CodigoAgencia+",\"CODIGO_USUARIO\":\""+usuario.Nombre+"\" ,\"CODIGO_CATEGORIA\":\""+CodigoCategoria+"\"}") },
};
HttpContent myBody = new FormUrlEncodedContent(values);*/
var formData = new MultipartFormDataContent();
String url = DataEntityLayer.Database.Environment.getFinalUrl(Util.UtilWS.subirImagenesCategorias);
var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(url, formContent);
string stringContent = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<CustomJsonResult>(stringContent);
}
catch (Exception ex)
{
result.Error = ex.Message;
}
return result;
}
This is how I tested the WS from postman
After many attempts and almost having a mental & emotional breakdown, it finally worked.
Example 3 from this link worked for me.
public static UsuarioSesion usuario = new UsuarioSesion();
public static async Task<CustomJsonResult> SubirImagenCategoría(int CodigoCategoria, HttpPostedFileBase Archivo)
{
usuario = UtilClass.GetUsuarioSesion();
var modelo = new SUBIR_IMAGEN_CAT();
modelo.CODIGO_AGENCIA = usuario.CodigoAgencia;
modelo.CODIGO_USUARIO = usuario.Nombre;
modelo.CODIGO_CATEGORIA = CodigoCategoria;
CustomJsonResult result = new CustomJsonResult();
try
{
var JsonString = JsonConvert.SerializeObject(modelo);
var formContent = new MultipartFormDataContent();
HttpContent JsonParam = new StringContent(JsonString);
formContent.Add(JsonParam, "JsonString");
var fileContent = new StreamContent(Archivo.InputStream);
fileContent.Headers.Add("Content-Type", "application/octet-stream");
fileContent.Headers.Add("Content-Disposition", "form-data; name=\"Archivo\"; filename=\"" + Archivo.FileName.ToString() + "\"");
formContent.Add(fileContent, "file", Archivo.FileName.ToString());
String url = DataEntityLayer.Database.Environment.getFinalUrl(Util.UtilWS.subirImagenesCategorias);
var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(url, formContent);
string stringContent = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<CustomJsonResult>(stringContent);
}
catch (Exception ex)
{
result.Error = ex.Message;
}
return result;
}
I have uploaded a file to SharePoint and found out what id it has. Now I need to update some of the other columns on that listitem. The problem is that System.Net.Http.HttpMethod.Patch doesn't exist.
public static async Task<string> UpdateFileData()
{
var (authResult, message) = await Authentication.AquireTokenAsync();
string updateurl = MainPage.rooturl + "lists/edd49389-7edb-41db-80bd-c8493234eafa/items/" + fileID + "/";
var httpClient = new HttpClient();
HttpResponseMessage response;
try
{
var root = new
{
fields = new Dictionary<string, string>
{
{ "IBX", App.IBX }, //column to update
{ "Year", App.Year}, //column to update
{ "Month", App.Month} //column to update
}
};
var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
var content = JsonConvert.SerializeObject(root, s);
var request = new HttpRequestMessage(HttpMethod.Put, updateurl);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
response = await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
catch (Exception ex)
{
return ex.ToString();
}
}
Modify the code as below.
public static async Task<string> UpdateFileData()
{
var (authResult, message) = await Authentication.AquireTokenAsync();
string updateurl = MainPage.rooturl + "lists/edd49389-7edb-41db-80bd-c8493234eafa/items/" + fileID + "/";
var httpClient = new HttpClient();
HttpResponseMessage response;
try
{
var root = new
{
fields = new Dictionary<string, string>
{
{ "IBX", App.IBX }, //column to update
{ "Year", App.Year}, //column to update
{ "Month", App.Month} //column to update
}
};
var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
var content = JsonConvert.SerializeObject(root, s);
var request = new HttpRequestMessage(new HttpMethod("PATCH"), updateurl);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
request.Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json;odata=verbose");
response = await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
catch (Exception ex)
{
return ex.ToString();
}
}
Or we can also use REST API to update list item by ID.
Refer to: SharePoint 2013 REST Services using C# and the HttpClient
It dependents whether .NET Core or .NET Framework is utilized, in case of `.NET Core HttpClient.PatchAsync Method could be utilized.
In case of .NET Framework ListItem could be updated like this:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
client.BaseAddress = new Uri("https://graph.microsoft.com");
var listItemPayload = new Dictionary<string, object>
{
{"Color", "Fuchsia"},
{"Quantity", 934}
};
var requestContent = new StringContent(JsonConvert.SerializeObject(listItemPayload));
requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await client.PatchAsync(new Uri($"https://graph.microsoft.com/v1.0/sites/{siteId}/lists/{listId}/items/{itemId}/fields"), requestContent);
var data = response.Content.ReadAsStringAsync().Result.ToString();
}
where PatchAsync is the extension method for HttpClient class:
public static class HttpClientExtensions
{
public static async Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent iContent)
{
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri)
{
Content = iContent
};
HttpResponseMessage response = new HttpResponseMessage();
try
{
response = await client.SendAsync(request);
}
catch (TaskCanceledException e)
{
Debug.WriteLine("ERROR: " + e.ToString());
}
return response;
}
}
All the credits for extension method go to the author of this answer
Can't you just use the HttpMethod class constructor?
new HttpMethod("PATCH");
Source: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpmethod.-ctor?view=netframework-4.7.2#System_Net_Http_HttpMethod__ctor_System_String_
I'm working on UWP apps, In that i need to upload the image for that i'm calling the web service to post the details. I'm using the below code to serialize the image.
byte[] fileBytes = null;
using (var stream = await file.OpenReadAsync())
{
fileBytes = new byte[stream.Size];
using (var reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}
docs.Document = fileBytes;
docs.DocumentName = file.Name;
docs.DocumentTypeOtherDescription = "ProfilePicture";
var docsAsJson = JsonConvert.SerializeObject(docs);
StringContent stringContent = new StringContent(docsAsJson, System.Text.Encoding.UTF8);
ByteArrayContent byteContent = new ByteArrayContent(fileBytes);
MultipartFormDataContent httpContent = new MultipartFormDataContent();
httpContent.Add(byteContent, file.Name);
httpContent.Add(stringContent);
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage {Method = HttpMethod.Post};
request.Headers.Add("authorization", App.TokenType + " " + App.AccessToken);
request.RequestUri = new Uri(App.BaseUrl + "api/User/UploadUserDocument");
request.Content = httpContent;
request.Content.Headers.Add(#"Content-Length", fileBytes.Length.ToString());
var response = httpClient.SendAsync(request).Result;
var data = response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
}
}
This is my side to serialize the image and in service side they deserialize the image and saved in database. But i'm getting StatusCode: 500, ReasonPhrase: 'Internal Server Error' this error, Any one please help me to solve this issue.
Here is my Service Code:
public IHttpActionResult UpdateUserWithProfilePic(FormData userviewmodel)
{
var error = string.Empty;
var userJson = new StringBuilder();
foreach (var items in userviewmodel.Fields)
{
if (items.Name == "}")
{
if (!userJson.ToString().EndsWith(",")) continue;
userJson.Remove(userJson.Length - 1, 1);
userJson.Append("}");
}
else
userJson.Append((items.Name.Replace("%22", "\"")) + ":" + items.Value);
}
var userView = JsonConvert.DeserializeObject<UserViewModel>(userJson.ToString());
var result = UpdateUser(userView, error);
if (result.ResultType != ResultType.Success) return daHttpActionResult(result.Result, result);
if (userviewmodel.Files != null && userviewmodel.Files.Count > 0)
{
userView.ProfileDocument = new UserDocument
{
DocumentName = userviewmodel.Files[0].Name,
Document = userviewmodel.Files[0].Value.Buffer,
UserID = UserId,
DocumentType = DocumentTypeEnum.ProfilePicture,
DocumentTypeOtherDescription = userviewmodel.Files[0].Value.MediaType,
};
}
return AdHttpActionResult(result.Result, result);
}
Thanks & Regards,
Cristina
I am struggling to use HttpClient to post FCM message. I am getting invalid Header format exception. I can use WebRequest to post FCM message. But I want to try as Async and Await by using HttpClient.
Please suggest me which is best HttpClient or WebRequest.
<div>
<br>String uri;
<br>uri = "https://fcm.googleapis.com/fcm/send";
<br>var postData = new
<br>{
<br>to = DeviceID,
<br>data = new
{
MessageID = enquiryid
},<br>
<br>notification = new
{
body = enquirymessage,
title = FromUser,
icon = "myicon"
}<br>
};<br>
<br>var serializer = new JavaScriptSerializer();
<br>var json = serializer.Serialize(postData);<br>
<br>using (var client = new HttpClient())
<br>{
<p>client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue
("key", "=" + fcmDetails.SERVER_API_KEY);<br>
<p>client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue
("application/json"));
<p>client.DefaultRequestHeaders.Add("Sender: id ", "="+ fcmDetails.PROJECT_KEY);
<p>using (var r = client.PostAsJsonAsync(new Uri(uri), json))
<br>{
<br>string result = await r.Content.ReadAsStringAsync();
<br>sResponseFromServer= result;
}<br>
}
Replace the line in your code
client.DefaultRequestHeaders.Add("Sender: id ", "="+ fcmDetails.PROJECT_KEY);
with
client.DefaultRequestHeaders.Add("Sender", "id=" + fcmDetails.PROJECT_KEY);
Use the sample code
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://fcm.googleapis.com/fcm/");
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("key", "=" + fcmDetails.SERVER_API_KEY);
client.DefaultRequestHeaders.Add("Sender","id=" + fcmDetails.PROJECT_KEY);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
var data = new
{
to = DeviceID,
notification = new
{
body = "This is the message",
title = "This is the title",
icon = "myicon"
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
request.Content = new StringContent(json,
Encoding.UTF8,
"application/json");//CONTENT-TYPE header
var data1 = client.PostAsync("send", request.Content);
var d = data1.Result.Content.ReadAsStringAsync();