I'm trying to add an attachment to a note using the set_note_attachment method. I'm calling the REST API from a .NET Core WebAPI. I can already login and create notes without any issue, but when I call the set_note_attachment it returns { "id": "-1" }, and I can't figure what's wrong.
I'm receiving the file in my controller and getting the byte array from the file like this:
byte[] fileUpload;
using (var memoryStream = new MemoryStream())
{
await file.CopyToAsync(memoryStream);
fileUpload = memoryStream.ToArray();
}
After that I convert the fileUpload array to a base64 string like this:
var sendFile = System.Convert.ToBase64String(file.Data);
I'm creating a dto to send the file to the method:
public async void UploadAttachments(string sessionId, string protocoloId, Sugar_FileDTO[] attachments)
{
//sessionId = result from login method
//protocoloId = note ID
foreach (var file in attachments) //Don't mind this, I'm sending only 1 file
{
var dto = new Sugar_SetNoteDTO(
sessionId,
new Note(protocoloId, file.FileName, System.Convert.ToBase64String(file.Data))
);
var result = await Request<Object>(dto, "set_note_attachment");
}
}
private async Task<T> Request<T>(Object data, string method)
{
var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("method", method),
new KeyValuePair<string, string>("input_type", "JSON"),
new KeyValuePair<string, string>("response_type", "JSON"),
new KeyValuePair<string, string>("rest_data", JsonConvert.SerializeObject(data, serializerSettings))
});
var resp = await httpClient.PostAsync(sugarCrmData.Url, content);
var respContent = await resp.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(respContent, serializerSettings);
}
I don't get what's wrong. I couldn't find anything relevant in the docs and nothing around the web.
I'm using version 7.9.
Thanks in advance!