简体   繁体   中英

WebApi HttpPost not receiving the posted content from HttpClient

Web API: I tried using [FormBody] and [FromForm] before the string stringcontent as well.

// POST api/<MAUserController>
        [HttpPost("AuthenticateUser")]
        public async Task<ActionResult<MAUser>> PostAsync(string stringcontent)
        {
            //stringcontent is null
        }

Client Code:

List<KeyValuePair<string, string>> postParameters = new List<KeyValuePair<string, string>>();
            postParameters.Add(new KeyValuePair<string, string>("Email", Email));
            postParameters.Add(new KeyValuePair<string, string>("Password", Password));
            var jsonString = JsonConvert.SerializeObject(postParameters);
            var stringContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenString);
                //httpClient.DefaultRequestHeaders.Add("Email", Email);
                //httpClient.DefaultRequestHeaders.Add("Password", Password);
                using (var response = await httpClient.PostAsync(API_URL, stringContent))
                {
                    if (response.IsSuccessStatusCode)
                    {
            //Success code
                    }
                    else
                    {
            //Handle unsuccessful here
                    }
                }

You're posting structured data in JSON format from the client. So why is the API just trying to accept a plain string? That doesn't make a lot of sense.

You should make it accept a model object with the correct structure, instead. ASP.NET will take care of binding the JSON properties to the model properties. However, you should also simplify the postParameters in the client as well - you're over-complicating the structure.

eg

Client:

var postParameters  = new { Email = "abc@example.com", Password = "xyz" };

Server:

public async Task<ActionResult<MAUser>> PostAsync([FromBody] UserCredentials credentials)
{
}

where UserCredentials is a DTO class like this:

public class UserCredentials
{
  public string Email { get; set; }
  public string Password { get; set; }
}

ok so when using content type you are basically telling the receiving API how to parse the request payload and so by telling it application/json the framework is trying to parse the payload from JSON to an object and assign it to your parameter ( not really since you need to add an attribute [FromBody] ) but it can not do it since you are expecting a string, so either change your content type or change your parameter type ( i would suggest your parameter type ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM