AddJsonBody()创建格式错误的Json

时间:2017-04-28 21:01:26

标签: c# json restsharp

我有一个简单的类,我试图通过RestSharp的AddJsonBody()

添加到POST调用

虽然在运行请求:\"The request body was not well-formed."\

时仍然遇到以下异常

这是有问题的Json:

{"Id":null,"Profile":{"FirstName":"Fraiincis","LastName":"Doeor","Email":"De3leteMePls@gmail.com","Login":"De3leteMePls@gmail.com"},"Credentials":{"Password":{"Value":"myPassword"}}}

奇怪的是,如果我将其粘贴到Postman中,它会显示最后3个花括号是问题。但是,如果我只是手动添加格式,那很好。或者,如果我粘贴到Json Formatter,它告诉我没关系。

我无法弄清楚这里发生了什么,以及如何解决它。

这是我的代码。我非常密切地跟着这个https://github.com/restsharp/RestSharp/wiki/Recommended-Usage

    public Task<IRestResponse<CustomerIdentity>> AddNewCustomerAsync(CustomerIdentity newCustomer)
    {
        Require.Argument("email", newCustomer.Profile.Email);

        var request = new RestRequest(resource: "{ApiVersion}/users?activate=true", method: Method.POST);
        request.AddParameter(name: "ApiVersion", value: Config.Version, type: ParameterType.UrlSegment);

        request.AddJsonBody(newCustomer);


        return ExecuteAsync<CustomerIdentity>(request);
    }

    public async Task<IRestResponse<T>> ExecuteAsync<T>(IRestRequest request) where T : class, new()
    {
        var client = new RestClient(baseUrl: Config.BaseUrl);

        request.AddHeader("authorization", "{ApiKey}");
        request.AddParameter(name: "ApiKey", value: Config.ApiKey, type: ParameterType.HttpHeader);

        var taskCompletionSource = new TaskCompletionSource<IRestResponse<T>>();
        client.ExecuteAsync<T>(request, restResponse =>
        {
            if (restResponse.ErrorException != null)
            {
                const string message = "Error retrieving response. Check inner details for more info";
                var oktaException = new ApplicationException(message, restResponse.ErrorException);
                throw oktaException;
            }
            taskCompletionSource.SetResult(restResponse);
        });

        return await taskCompletionSource.Task;
    }

我正在使用RestSharp 105.2.3.0 BTW

0 个答案:

没有答案