user1181942
user1181942

Reputation: 1607

"Bad request" error when passing DateTime to REST API

Bad request errorI am sending data to HTTP post API. But everytime I try to call the API, I get error code: 400, Bad request message.

Here is my API code:

  [Route("InsUpPlayer")]
    [HttpPost]
    public async Task<object> InsUpPlayer([FromForm] Players player)
    {
        try
        {

            //Some code here

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

And my repository code:

public async Task<string> PlayerInsUpPost(Player player1)
    {
        var SendResponse = "false";
        try
        {
            var RequestUrl = baseUrl + "Master/InsUpPlayer";
            var httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(RequestUrl);

            using (var player = new MultipartFormDataContent())
            {
                if (player1.ProfileImageFile != null)
                {
                    string objimgFileBase64 = "";
                    ByteArrayContent fileContent;
                    using (var ms = new MemoryStream())
                    {
                        player1.ProfileImageFile.CopyTo(ms);
                        var fileBytes = ms.ToArray();
                        objimgFileBase64 = Convert.ToBase64String(fileBytes);
                    }
                    byte[] bytes = Convert.FromBase64String(objimgFileBase64);
                    fileContent = new ByteArrayContent(bytes);
                    player.Add(fileContent, "ProfileImageFile", string.Format("{0}", player1.ProfileImageFile.FileName));
                }
                if (player1.DetailImageFile != null)
                {
                    string objimgFileBase64 = "";
                    ByteArrayContent fileContent;
                    using (var ms = new MemoryStream())
                    {
                        player1.DetailImageFile.CopyTo(ms);
                        var fileBytes = ms.ToArray();
                        objimgFileBase64 = Convert.ToBase64String(fileBytes);
                    }
                    byte[] bytes = Convert.FromBase64String(objimgFileBase64);
                    fileContent = new ByteArrayContent(bytes);
                    player.Add(fileContent, "DetailImageFile", string.Format("{0}", player1.DetailImageFile.FileName));
                }
                player.Add(new StringContent(player1.playerId.ToString()), "playerId");
                player.Add(new StringContent(player1.FirstName), "FirstName");
                player.Add(new StringContent(player1.LastName), "LastName");
                player.Add(new StringContent(player1.DOB.ToString()), "DOB");
                player.Add(new StringContent(player1.Nationality.ToString()), "Nationality");
                player.Add(new StringContent(player1.BirthState.ToString()), "BirthState");
                player.Add(new StringContent(player1.JerseyNo.ToString()), "JerseyNo");
                player.Add(new StringContent(player1.Postion.ToString()), "Postion");
                player.Add(new StringContent(player1.Biography), "Biography");
                player.Add(new StringContent(player1.isActive.ToString()), "isActive");                    
                player.Add(new StringContent(player1.isPublish.ToString()), "isPublish");
                player.Add(new StringContent(player1.UserType.ToString()), "UserType");

                HttpResponseMessage objResponse = await httpClient.PostAsync(RequestUrl, player);
                if (objResponse.IsSuccessStatusCode && (int)objResponse.StatusCode == 200)
                {
                    var serResponse = objResponse.ContentAsType<ResultModel>();
                    //SendResponse = serResponse.result;
                    SendResponse = "true";
                }
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Exception Occured");
            throw;
        }
        return SendResponse;
    }

The Player class is like this:

public class Player
{
    public long playerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }
    public int Nationality { get; set; }
    public int BirthState { get; set; }
    public int JerseyNo { get; set; }
    public int Postion { get; set; }
    public string Biography { get; set; }
    public bool isActive { get; set; }
    public bool isPublish { get; set; }
    public int UserType { get; set; }
    public IFormFile ProfileImageFile { get; set; }
    public IFormFile DetailImageFile { get; set; }

    public string ProfileImage { get; set; }
    public string DetailImage { get; set; }
}

Update: Here is my JQuery code: The DOB here is correct, but I realized just now that it is not getting passed correctly to the controller.

$("#PublishPlayer").click(function () {
debugger;
var value = $('#CreatePlayerForm').valid();
var url = '/Admin/PlayerInsUpPost';

var day = $('#Day').val();
var month = $('#Month').val();
var year = $('#Year').val();

var DOB = new Date(year, month, day);

var fdata = new FormData();
fdata.append("playerId", $('#playerId').val());
fdata.append("FirstName", $('#FirstName').val());
fdata.append("LastName", $('#LastName').val());
fdata.append("DOB", DOB);
fdata.append("Nationality", $('#Nationality').val());
fdata.append("BirthState", $('#BirthState').val());
fdata.append("JerseyNo", $('#JerseyNo').val());
fdata.append("Position", $('#Position').val());
fdata.append("Biography", $('#Biography').val());


fdata.append('ProfileImageFile', $('#ProfileImageFile')[0].files[0]);

fdata.append('DetailImageFile', $('#ProfileImageFile')[0].files[0]);

if (value == true) {
    $.ajax({
        url: url,
        datatype: "json",
        accept: {
            javascript: 'application/javascript'
        },
        type: "POST",
        cache: false,
        processData: false,
        contentType: false,
        data: fdata,
        success: function (result) {
            if (result == "true") {
                alert('Player added successfully.');
                window.location.href = "/Admin/PlayerList";

            } else if (result == "false") {
                alert('Failed to update, please try later.');
            }
        },
        error: function () {
            alert('Something went wrong');
        }
    });
}
else {
    //$('.playeradd').removeClass('show');
    //$('.playeradd').addClass('hide');
    return false;
}
//event.stopPropagation();

});

The DOB in JQuery before calling Ajax is : Wed Sep 12 2001 00:00:00 GMT+0530 (India Standard Time) {}

When passed to controller it is: {01-01-0001 12:00:AM}

If I comment DOB in API and in the frontend, everything works fine. But I need to send DOB to API and I can't change the datatype of DOB in API. How do I fix this error?

Upvotes: 2

Views: 3085

Answers (3)

Szymon Markiewicz
Szymon Markiewicz

Reputation: 21

I suppose it is some problem with ASP.NET deserialization of DateTime (I'm not sure tho). I have run into similar problem of sending dates before and my solution was instead of sending DateTime struct, I send number of ticks (which you can get from DateTime object) as long variable.

DateTime BOD = DateTime.Now; // You may fill your DateTime object with your own data
long BODticks = BOD.Ticks;

And then on the server side you can easily recreate date time like this:

DateTime myDate = new DateTime(BODticks);

And then in order to use it you can modify your player class as follows:

public class Player
{
    public long playerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public long DOBTicks { get; set; }
    public DateTime DOB { get => new DateTime(DOBTicks); set => DOBTicks = value.Ticks; }
    public int Nationality { get; set; }
    public int BirthState { get; set; }
    public int JerseyNo { get; set; }
    public int Postion { get; set; }
    public string Biography { get; set; }
    public bool isActive { get; set; }
    public bool isPublish { get; set; }
    public int UserType { get; set; }
    public IFormFile ProfileImageFile { get; set; }
    public IFormFile DetailImageFile { get; set; }

    public string ProfileImage { get; set; }
    public string DetailImage { get; set; }
}

I'm sure someone could find a better solution though and that's assuming this one actually works and I understood your problem right.

Upvotes: 2

iikkoo
iikkoo

Reputation: 2856

The new serializer in .net core > 3.0 is strict when parsing date formats (note that the default has changed from newtonsoft json). They have to be in ISO8601 format, i.e. YYYY-MM-DD. If you are passing something that isn't in ISO8601 forms you have to write a custom formatter.

public class DateTimeJsonConverter : JsonConverter<DateTime>
{
    public override DateTime Read(
        ref Utf8JsonReader reader,
        Type typeToConvert,
        JsonSerializerOptions options) =>
            DateTime.ParseExact(reader.GetString(),
                "<YOUR FORMAT HERE>", CultureInfo.InvariantCulture);

    public override void Write(
        Utf8JsonWriter writer,
        DateTime dateTimeValue,
        JsonSerializerOptions options) =>
            writer.WriteStringValue(dateTimeValue.ToString(
                "<YOUR FORMAT HERE>", CultureInfo.InvariantCulture));
}

The code above is an example of a custom formatter.

Read further details here (https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to#sample-basic-converter) in how to create a custom formatter for your input.

Upvotes: 1

Ihusaan Ahmed
Ihusaan Ahmed

Reputation: 533

When passing the data into the ajax request convert it to ISO string. Dotnet understands that. So do something like this:

fdata.append("DOB", DOB.toISOString());

Upvotes: 2

Related Questions