关于#c##的问题:请求为post ,参数为body中raw

要求使用nodejs或C#写一个请求参数为如下格式的接口。

请求为post ,参数为body中raw,只要能拿到三个值即可。如下为参数:

{
    "data""{\"LightweightName\":\"SS\",\"status\":\"0\",\"Msg\":\"\"}"
}

img

大概达到的效果

img

使用System.Text.Json或者Newtonsoft.Json将POST请求中的data反序列化成对应的对象即可,.NET 6(C#)的实现示例如下:

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace RequestRawDemo.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class UploaderController : ControllerBase
    {

        [HttpPost]
        public LightWeight? Upload([FromBody] LightWeightRequest request)
        {
            return request.LightWeight;
        }
    }

    public class LightWeightRequest
    {
        public string? Data { get; set; }
        public LightWeight? LightWeight => Data != null ? JsonConvert.DeserializeObject<LightWeight>(Data) : new LightWeight();
    }
    public class LightWeight
    {
        public string? LightweightName { get; set; }
        public string? Status { get; set; }
        public string? Msg { get; set; }
    }
}

运行效果如下:

img

img

参考如下链接试试https://blog.csdn.net/qq_44858151/article/details/110421367

不知道你到底是要实现接口,还是要调用的接口的代码。下面是一个简单的api实现示例。


    [ApiController]
    [Route("[controller]")]
    public class HomeController : Controller
    {

        [HttpPost]
        public string Index([FromBody] Data data)
        {
            //获取参数
            Console.WriteLine("paras: " + data.LightweightName);
            //返回json
            return JsonConvert.SerializeObject(new { code = 0, msg = new { }, data = new { } });
        }
    }


    public class Data
    {
        public string LightweightName { get; set; }

        public string status { get; set; }

        public string Msg { get; set; }
    }

.net MVC接口FromBody修饰一下实体参数 就行了,然后直接读取类属性就行了