0

I am trying to use fetch from the client side to request a chat session. In my controller I am attempting to capture (or bind) the request body to a model. Then, my controller is using the model to make an API call using RestSharp. The problem is that my parameter is not getting the values of the request body.

Here is how it looks:

// The fetch from the view
const requestBody = buildRequestSession()
var url = '@Url.Action("RequestSession", "Sdk")'
const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
            },
            body: JSON.stringify(requestBody)
        })

This is the payload of the fetch

Image of the request payload

// Controller action
[HttpPost]
public async Task<IActionResult> RequestSessionAsync([FromBody]RequestSession requestSession)
{
    RestRequest request = new RestRequest("sessions");
    RestResponse response = await _restClient.ExecuteGetAsync(request);

    RequestSessionResponse? sessionResponse = JsonConvert.DeserializeObject<RequestSessionResponse>(response.Content!);

    return Json(sessionResponse);
}

This is the result of the fetch:

Image of the parameter when fetch hits controller action

// model for my controller action parameter
public record RequestSession
{
    [JsonProperty("businessUnitName")]
    internal string BusinessUnitName { get; set; } = string.Empty;
    [JsonProperty("caption")]
    internal string Caption { get; set; } = string.Empty;
    [JsonProperty("contactData")]
    internal ContactData[]? ContactData { get; set; }
    [JsonProperty("customerName")]
    internal string CustomerName { get; set; } = string.Empty;
    [JsonProperty("destination")]
    internal string Destination { get; set; } = string.Empty;
    [JsonProperty("language")]
    internal string Language { get; set; } = string.Empty;
    [JsonProperty("priority")]
    internal int Priority { get; set; }
    [JsonProperty("source")]
    internal string Source { get; set; } = string.Empty;
}

My setup: ASP.NET Core 6 MVC web application using Visual Studio 2022.

I understand this may appear to be a duplicate question, however, none of the suggestions in the following posts have resolved my issue.

Similar question

Another similar question

I have restructured my fetch, model, and controller action in various ways to no avail. All of the suggestions mentioned in other SO posts have not worked. Any explanation on how this is suppose to work will be appreciated.

5
  • What is the request body and what is the response body? Does the response http header status a 200 OK or and Error? Commented Apr 11, 2023 at 16:19
  • Why are properties internal? Have you tried with public? Commented Apr 11, 2023 at 17:03
  • @jdweng, the response has an unrelated error. Commented Apr 11, 2023 at 18:48
  • @RicardoPeres, properties are internal to keep scope internal to my application. I will try making them public, but shouldn't it still work either way? Commented Apr 11, 2023 at 18:49
  • 1
    @RicardoPeres, that was the issue. If you want to provide an answer and explain why this is the case I will accept it. I guess I'm confused now on why it binds when the fields are public but not when they are internal. Commented Apr 11, 2023 at 19:12

1 Answer 1

1

The properties need to be public, otherwise, the binder won't find them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.