Link to home
Create AccountLog in
Avatar of yadavdep
yadavdepFlag for India

asked on

Dot net core api is not getting value

I have a dot net core API which has a method
Now from PostMan when I am testing this api and sends this json data
"{
    "Username": "xxx116@clsdsddp.com"
}"
I always gets null in the username.

I have set the content type to application/json.

Not sure where I am making the mistake



using Autofac.Multitenant;
using CLIPITC.CORE.Helper;
using CLIPITC.CORE.Middlewares;
using CLIPITC.Repositories.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using System;
using System.Threading.Tasks;
using Utils;
using static System.Net.WebRequestMethods;

namespace CLIPITC_CORE.Controllers
{
    [ServiceFilter(typeof(CustomActionFilter))]
    [Produces("application/json")]
    [Route("~/api/[Action]")]
    public class AccountController : Controller
    {
        IAccountRepository _accountRepo;
        public AccountController(IAccountRepository accountRepo, ITenantIdentificationStrategy service)
        {
            _accountRepo = accountRepo;
            (service as MultitenantResolver).Init(_accountRepo);
        }

        [HttpPut]
        public ActionResult RemoveMobileIDForUser(string Username)
        {
            
            if (Username.IsNull())
                return new BadRequestObjectResult("UserName Can't be empty");

            var response = _accountRepo.RemoveRememberMeOptionFromUser(Username);
            return new ObjectResult(response);
        }
    }
}

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

I always gets null in the username.

are you getting a response status 200 in your Postman request?

User generated image

try debug the object : response before you return it.

how's the expected JSON response looks like?
Avatar of yadavdep

ASKER

I am getting an Bad request error that is because of code written in the method
 if (Username.IsNull())
                return new BadRequestObjectResult("UserName Can't be empty");

Open in new window


the strange part is if I remove the content type from the request then it works without any issue.
The expected is just true or false {Result:true}
I am getting an Bad request error that is because of code written in the method

Ok, think you are getting an 404 error here.

so you need to make sure you pass in the correct header and body request to your web service
in your declared method:

[HttpPut]
        public ActionResult RemoveMobileIDForUser(string Username)

Open in new window

So, make sure your request is using PUT for post request.

User generated image
Yes, I checked that part already
Also,

Like I said before also
the strange part is if I remove the content type from the request then it works without any issue.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Yes, Postman is also checked
Postman is also checked                                   
so now you got a bad request 404 returned or with a response with status 200?
I got it working
I was sending the Json in wrong form.

I jus sent "ddd@dasdadsa.com" and it worked