E
E
ebaysher2022-01-12 15:28:47
API
ebaysher, 2022-01-12 15:28:47

What is wrong with POST request?

Good afternoon, in front-end development quite recently, so please treat with understanding
The problem is this:
I send a post request to the web api with the entered data and an error occurs with status 400.
The payload is empty, in response such an error The JSON value could not be converted to air_astana_webapi.Models.AirLine. Path: $ | LineNumber: 0 | BytePositionInLine: 1."

Here is the service code:

addAirline(data:any)
  {
   readonly airlineAPIUrl = 'https://localhost:44336/api'
    debugger
    return this.http.post(this.airlineAPIUrl + '/airlines', data);
  }


code from the form component from where the data is entered:

AddAirline()
  {
    debugger
    this.airLine.Id != 0;
    this.airLine.companyName = this.company;
    this.airLine.airLineStatus = this.status;
    this.airLine.whereFrom = this.where;
    this.airLine.whereFromTime = moment(this.timeWhere,"HH:mm").toISOString();
    this.airLine.from = this.from;
    this.airLine.fromTime = moment(this.timeFrom,"HH:mm").toISOString();
    this.airLine.airLineDelayTime = this.delay;
    
    this.service.addAirline(this.airLine).subscribe(res => {this.refreshAirlinesList()}, error=> {console.log(error)/*alert('Error')*/});

  }


post method in controller:
[HttpPost]
        [Authorize]
        public async Task<ActionResult<AirLine>> PostAirLines(AirLine airLine)
        {
            if (airLine == null)
            {
                return BadRequest();
            }
            else
            _context.AirLines.Add(airLine);
            await _context.SaveChangesAsync();
            return CreatedAtAction("GetAirline", new { id = airLine.Id }, airLine);
        }

And a class in api with these properties
public class AirLine
    {
        public int Id { get; set; }

        public string CompanyName { get; set; }

        [StringLength(30)]
        public string AirLineStatus { get; set; } = string.Empty;

        [StringLength(100)]
        public string WhereFrom { get; set; } = string.Empty;
        public DateTime WhereFromTime { get; set; }

        [StringLength(100)]
        public string From { get; set; } = string.Empty;
        public DateTime FromTime { get; set; }

        public int AirLineDelayTime { get; set; }
    }


All parameters in the post request correspond to those in the airline class

I would appreciate any help and hints.
61dec91927da2276569196.png61dec92e5146b924901082.jpeg

In swager, everything works as it should and the problem is in the front-end part, as I understand it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Voland69, 2022-01-12
@Voland69

On the screen Id: null, while in the class int - which cannot be null.
Well, perhaps you can check the serialization of DateTime.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question