0

I want to send a post request (using postman) to Asp.net core web API and my API is like this:

[HttpPost]
public IActionResult Create(CreateTransferItemCommand command)
{
    try
    {
        _transferCommandFacade.CreateTransferItem(command);
        return Ok(command);
    }
    catch (Exception e)
    {
        return BadRequest(e.Message);
    }
}

And my CreateTransferItemCommand model is like this:

public class CreateTransferItemCommand : Command
{
    public Guid TransferId { get; set; }
    public int AssetNumber { get; set; }
    public int PartId { get; set; }
    public int EmployeeId { get; set; }
    public string Description { get; set; }
    public long StuffDocId { get; set; }
}

I have below code in startup.cs and ConfigureServices method:

services.AddMvc()
    .AddJsonOptions(options =>
    {
        options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
    })
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

And I set request body in postman like this:

{
  "transferId":"fd3f3cd5-f52f-4b8e-b479-7bfe6dc6dc5f",
  "assetNumber":"",
  "partId":"",
  "employeeId":"",
  "description":"",
  "stuffDocId":""
}

The problem is that I can set "int" properties to empty string but I can not set long property (stuffDocId) to empty string! If I set stuffDocId to empty string postman responses like this:

{
    "errors": {
        "stuffDocId": [
            "Error converting value \"\" to type 'System.Int64'. Path 'stuffDocId', line 1, position 179."
        ]
    },
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "0HLOEDQM8HLQS:0000000A"
}

If I set stuffDocId to null it's working. But I want to set it to empty string. How can I solve this problem?! And why int and long don't have same behavior in this situation?

Armin Kianian
  • 143
  • 2
  • 10
  • Hi Armin, to solve this I need to ask, must stuffDocId be a long datatype?. You can change the data type to maybe a string then when you intend to use it, you cast it to a long or better still use a double and take the double quotes off when in postman. – Chsiom Nwike Jul 22 '19 at 07:24
  • @OgeNwike Hi Oge, I validate stuffDocId in domain and I don't want to change my domain and its unit tests. So I have to use long – Armin Kianian Jul 22 '19 at 07:33

0 Answers0