I have created one web api in C# where one action method takes object as parameter which includes IFormFile and sends the file for printing. But every time I am calling this function using some c# console app then I am getting this IFormFile as null.
Here is my code for API:
[Route("print")]
[HttpPost]
public IHttpActionResult Print([FromBody] PrintFile printDocument)
{
try
{
if (ModelState.IsValid)
{
var response = _printingService.PrintFile(printDocument.Document, printDocument.PrinterName, printDocument.ServerName);
return Ok(response);
}
else
{
return Unauthorized();
}
}
catch (Exception ex)
{
return BadRequest(string.Format(ErrorMessages.GeneralErrorMsg, ex.Message));
}
}
Model class:
public class PrintFile
{
public string PrinterName { get; set; }
public string ServerName { get; set; }
[Required]
public IFormFile File { get; set; }
public IDictionary<string, string> Parameters { get; set; }
}
C# client application calling this API method:
public ResponseData PrintFile(PrintFileInfo printFileInfo)
{
try
{
_request = new RestRequest(ResourceNames.Print, Method.POST);
_request.AddJsonBody(printFileInfo);
var response = _client.Execute(_request);
var responseData = new ResponseData()
{
ResponseCode = response.StatusCode.ToString(),
ResponseBody = response.Content
};
return responseData;
}
catch (Exception ex)
{
_logger.Error(ex, string.Format(ErrorMessages.ResourceException, ResourceNames.Print));
throw;
}
}
While sending through this C# client then I can see the IFormFile is filled in with data but when it goes to the API then it becomes null. Any suggestion would be appreciative.