【问题标题】:RESTful client in Unity - validation errorUnity 中的 RESTful 客户端 - 验证错误
【发布时间】:2020-03-23 19:43:54
【问题描述】:

我有一个使用 ASP.Net 创建的 RESTful 服务器,并尝试使用 Unity 的 RESTful 客户端连接到它。 GET 完美运行,但是在发送 POST 请求时出现验证错误。同时,从 Postman 发送请求时,GET 和 POST 都可以工作。 我的服务器:

        [HttpPost]
        public IActionResult Create(User user){
            Console.WriteLine("***POST***");
            Console.WriteLine(user.Id+", "+user.sex+", "+user.age);
            if(!ModelState.IsValid)
                return BadRequest(ModelState);
            _context.Users.Add(user);
            _context.SaveChanges();
            return CreatedAtRoute("GetUser", new { id = user.Id }, user);
        }

我的客户:

IEnumerator PostRequest(string uri, User user){
        string u = JsonUtility.ToJson(user);
        Debug.Log(u);
        using (UnityWebRequest webRequest = UnityWebRequest.Post(uri, u)){
            webRequest.SetRequestHeader("Content-Type","application/json");
            yield return webRequest.SendWebRequest();
            string[] pages = uri.Split('/');
            int page = pages.Length - 1;
            if (webRequest.isNetworkError || webRequest.isHttpError){
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }
            else{
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }
        }
    }

我尝试使用 Json 转换和自己编写字符串,也使用 WWWForm,但错误仍然存​​在。
该错误表明这是一个未知的 HTTP 错误。打印返回的文本时,它说:
"One or more validation errors occurred.","status":400,"traceId":"|b95d39b7-4b773429a8f72b3c.","errors":{"$":["'%' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}
在服务器端,它识别出正确的方法和控制器,但是,它甚至没有到达方法的第一行(Console.WriteLine)。然后它说:“执行 ObjectResult,写入 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails' 类型的值”。 以下是所有服务器端消息:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5001/user application/json 53
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'TheNewestDbConnect.Controllers.UserController.Create (TheNewestDbConnect)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "Create", controller = "User"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Create(TheNewestDbConnect.Data.Entities.User) on controller TheNewestDbConnect.Controllers.UserController (TheNewestDbConnect).
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action TheNewestDbConnect.Controllers.UserController.Create (TheNewestDbConnect) in 6.680400000000001ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'TheNewestDbConnect.Controllers.UserController.Create (TheNewestDbConnect)'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 11.3971ms 400 application/problem+json; charset=utf-8
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]

我不知道发生了什么以及如何解决它。任何帮助将不胜感激!

【问题讨论】:

    标签: rest unity3d client-server validationerror


    【解决方案1】:

    原来我只是缺少一个上传处理程序。添加这一行解决了它: webRequest.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(JsonObject));

    【讨论】:

      猜你喜欢
      • 2015-07-09
      • 1970-01-01
      • 2011-11-08
      • 2018-09-01
      • 2016-07-18
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多