HttpClient PostAsync() doesn't work while POSTMAN works fine

Viewed 1183

I'm trying to post an item on my Azure database account. I wrote and published to the Azure account an ASP.Net Core web application, which implements such methods as Get, Post, Put, and Delete. On my Xamarin Forms project, I'm trying to call HttpClient PostAsync method to create(post) a record in the Azure database. Testing the same transaction on POSTMAN works just fine, but it doesn't work in my Xamarin Forms mobile app. The GetStringAsync method works just fine both on POSTMAN and on my mobile app. I have no idea why the PostAync only doesn't work. Please help!

Here's the event handler method where PostAsync is called:

async void OnAdd(object sender, EventArgs e)
{
    // This item is to be posted to the Azure database.
    var tblmoney_item = new TblMoney
    {
        RecordDate = Convert.ToDateTime("7-26-2020"),
        ItemDesc = "coffee",
        AmountOut = 4500,    // The currency unit is in KRW. (KRW 4,500 = US$4).
        SpentTypeId = 3
    };

    // Serialize the item in JSON format.
    var content = JsonConvert.SerializeObject(tblmoney_item);

    // POST the item to the URL. Works fine on POSTMAN, but does NOT work on my mobile app.
    await _client.PostAsync(Url, new StringContent(content));
    
    // Refresh the list view.
    _tblmoney_items.Insert(0, tblmoney_item);
}

Here's the declaration within the MainPage class of the variables(properties) above:

private const string Url = "https://testwebapi.azurewebsites.net/tblmoney";
private HttpClient _client = new HttpClient();
private ObservableCollection<TblMoney> _tblmoney_items;

The item's Class desclaration is as the following:

public partial class TblMoney
{
    public int Id { get; set; }
    public DateTime? RecordDate { get; set; }
    public string ItemDesc { get; set; }
    public int? AmountOut { get; set; }
    public int? SpentTypeId { get; set; }

    public virtual TblSpentType SpentType { get; set; }
}

Here's the Controller Class in the REST Web API project, which is an ASP.Net Core Web Application project.

[ApiController]
[Route("[controller]")]
[Produces("application/json")]

public class TblMoneyController : ControllerBase
{
    private mynotedbContext _context = new mynotedbContext();

    [HttpGet]
    public IEnumerable<TblMoney> Get()
    {
        // get all tblMoney items.
        return _context.TblMoney.ToList();
    }

    [HttpPost]
    public IActionResult Post([FromBody]TblMoney tblmoney_item)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        _context.TblMoney.Add(tblmoney_item);
        _context.SaveChanges(true);
        return StatusCode(StatusCodes.Status201Created);
    }
}

The DB context looks like this:

public partial class mynotedbContext : DbContext
{
    public mynotedbContext()
    {
    }

    public mynotedbContext(DbContextOptions<mynotedbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<TblMoney> TblMoney { get; set; }
...
1 Answers

(From comments)

I recommend you set the Content-Type header to application/json - you can either do this as setting the header on the client or by using the overload of stringcontent that declares the type: e.g. new StringContent(content, Encoding.UTF8, "application/json"))

Related