2

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?

6
  • 1
    There's not a lot we can do without knowing more details. What are you POSTing to the API? What is in the actual response of the 400 error? Have you tried debugging line by line? Which line causes the problem?
    – DavidG
    Apr 26, 2021 at 16:30
  • Yes, I have tried debugging, The debugger in the API is never called, and that is why I haven't posted the API code. But, If I comment out the DOB.ToString() line in the repository, the API is called. updating question with proper error. Apr 26, 2021 at 16:36
  • I would suggest also posting an example of the JSON you are POSTing to the API. If the debugger is not hitting that method at all when you include the datetime field, sounds like a model binding problem. Apr 26, 2021 at 16:39
  • Ok, let me upload the example of the values I am posting. I am not sending JSON I guess.. I am not converting it to JSON anywhere. Apr 26, 2021 at 16:42
  • What version of net core are you using?
    – iikkoo
    Apr 26, 2021 at 18:13

3 Answers 3

2

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());
5
  • Thank you! I can now pass DOB to the controller, but the Bed request error is still there. Apr 26, 2021 at 17:31
  • 1
    Pass dob as string since when you pass as date format then it contains space char which makes request data invalid. Apr 26, 2021 at 18:09
  • @RahulShukla, I am passing DOB as a string, but still, the issue is there. Apr 27, 2021 at 5:29
  • hmm.. can you add the logs from the api to see what exactly is causing the 400 bad request Apr 27, 2021 at 8:13
  • function is not their for me Oct 2, 2021 at 2:12
2

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.

1
  • The issue is that I cannot change anything in API and API is accepting DOB in DateTime format, I am converting DateTime to string and sending it. Even when I am converting DOB to ticks, I am still sending everything in string format. I changed the Player in API just for testing and the API was accepting the input. So is there any way I can send datetime to API? Apr 27, 2021 at 5:27
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.