0

I am trying to retrieve post body with ASP.NET server but it is not receiving the body.

This is my controller

[HttpPost] public Boolean ReleaseProtection([FromBody]string value) { System.Diagnostics.Debug.WriteLine("returning value"); System.Diagnostics.Debug.WriteLine(value); return true; } 

This is my client c# script

var credentials = new Dictionary<string, string>() { {"token", Token.Value}, {"repoId", repoId} }; var content = new FormUrlEncodedContent(credentials); var response = await client.PostAsync(Ribbon1.DOTNETHOSTURL + "api/excel/ReleaseProtection", content); var responseString = await response.Content.ReadAsStringAsync(); 

If I use the same method to post to nodeJS server, it receives message fine but [FormBody] string value returns empty string. Why is this?

1 Answer 1

1

Here's a pretty in depth answer about how to use [FromBody] with a POST: Sending HTML Form Data

The short version is that you don't want to have a raw string value.

You should create a simple class like

public class MyCredentials { public string Token { get; set; } public string RepoId { get; set; } } 

And then you can read it like this

[HttpPost] public Boolean ReleaseProtection([FromBody]MyCredentials creds) { // Do stuff... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.