none
Getting BadRequest (400) error when sending post request by HttpClient RRS feed

  • Question

  •                                                  

    Post Sending Code:

    try       

    {                   

    var json = JsonConvert.SerializeObject(this.Current);                   

    Console.WriteLine(json);                   

    StringContent sc = new StringContent(json, Encoding.UTF8,"application/json");                                     

    var post = await this.client.PostAsync("api/customers", sc);                       

    if (post.IsSuccessStatusCode)                        {                           

    Uri lol = post.Headers.Location;                         

     var newcust = await this.client.GetAsync(lol);                           

    if (newcust.IsSuccessStatusCode)                            {                             

      var custData = await newcust.Content.ReadAsStringAsync();                               

    this.CopyCustomer(JsonConvert.DeserializeObject<Custumer>(custData),this.Current);                           

        this.OnPropertyChanged(nameof(Current));                               


    Controller Post:

     
    [ResponseType(typeof(Customer))]
            public async Task<IHttpActionResult> PostCustomer(Customer customer)
            {
    
    
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                customer.ModifiedDate = DateTime.Now;
                customer.rowguid = Guid.NewGuid();
                db.Customer.Add(customer);
    
                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateException)
                {
                    if (CustomerExists(customer.CustID))
                    {
                        return Content(HttpStatusCode.Conflict, $"The customer with id {customer.CustID} are existing. Please add with other id");
                    }
                    else
                    {
                        throw;
                    }
                }
    
    
    
                return CreatedAtRoute("DefaultApi", new { id = customer.CustID }, customer);
            }

    Please, Help me with this problem.

    Best Regards,

    Mammedli Elnur

                                                  
    • Moved by CoolDadTx Monday, June 4, 2018 2:41 PM ASP.NET related
    Sunday, June 3, 2018 7:12 AM

Answers

  • Hi Mammedli Elnur,

    For your question, it is related to ASP.NET. you should post your question in ASP.NET forum.The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Dolen

     

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 4, 2018 8:48 AM

All replies

  • ASP.NET WebAPI can be discussed at the ASP.NET forums.

    http://forums.asp.net/

    Sunday, June 3, 2018 3:15 PM
  • Hi Mammedli Elnur,

    For your question, it is related to ASP.NET. you should post your question in ASP.NET forum.The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Dolen

     

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 4, 2018 8:48 AM