从控制台应用程序使用RestSharp时收到空白值

时间:2019-06-25 23:14:45

标签: c# json restsharp

我创建了一个C#控制台应用程序,该应用程序从SQL Server调用存储过程,从尚未传输记录的数据库表中检索记录,并将所述记录添加到RestSharp请求中。当我在本地运行我的应用程序时,会收到“确定状态”,其中包含反映缺失信息的状态代码。 API的日志记录系统在收到的请求中显示空值。

我尝试了如下的RestSharp请求:

private static RestRequest CreateRestRequest(string resource, Method method)
    {

        var credentials = GetCredentials();

        var restRequest = new RestRequest { Resource = resource, Method = method, RequestFormat = DataFormat.Json, };
        restRequest.AddHeader("accept", "application/json");
        restRequest.AddHeader("Authorization", credentials);
        restRequest.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
        restRequest.OnBeforeDeserialization = resp => { resp.ContentEncoding = "utf-8"; };
        restRequest.OnBeforeDeserialization = resp =>
        {
            if (resp.RawBytes.Length >= 3 && resp.RawBytes[0] == 0xEF && resp.RawBytes[1] == 0xBB && resp.RawBytes[2] == 0xBF)
            {
                // Copy the data but with the UTF-8 BOM removed.
                var newData = new byte[resp.RawBytes.Length - 3];
                Buffer.BlockCopy(resp.RawBytes, 3, newData, 0, newData.Length);
                resp.RawBytes = newData;

                // Force re-conversion to string on next access
                resp.Content = null;
            }
        };
        Console.WriteLine(restRequest);
        return restRequest;
    }

用于请求值的逻辑如下:

private SignUpRequest BuildMerchantTestData()
    {
        var onboardingDAL = new OnboardingDAL(iconfiguration);

        var onboardingList = onboardingDAL.GetOnboardingList(iconfiguration);
        var ownerList  = new List<OwnerList>();


        if (onboardingList != null)
        {
            Debug.Assert(onboardingList != null, nameof(OnboardingList) + " != null");
            foreach (Onboarding result in onboardingList)
            {
                Console.WriteLine("{0} {1}", result.Email, result.UserId);
                List<Owner> owner1 = new List<Owner>{ new Owner
                {
                    FirstName = result.OwnerFirstName,
                    LastName = result.OwnerLastName,
                    Address = result.OwnerAddress,
                    City = result.OwnerCity,
                    State = result.OwnerRegion,
                    Zip = result.OwnerZipCode,
                    Country =result.OwnerCountry,
                    DateOfBirth = result.OwnerDob,
                    SSN = result.OwnerSsn,
                    Email = result.Email,
                    Percentage = result.OwnerPercentage,
                    Title = result.OwnerTitle
                }};
                var signupRequest = new SignUpRequest
                {
                    PersonalData = new PersonalData
                    {
                        FirstName = result.FirstName,
                        MiddleInitial = result.MiddleInitial,
                        LastName = result.Lastname,
                        DateOfBirth = result.DateOfBirth,
                        SocialSecurityNumber = result.Ssn,
                        SourceEmail = result.Email,
                        PhoneInformation =
                            new PhoneInformation
                                {DayPhone = result.DayPhone, EveningPhone = result.EveningPhone},

                    },
                    InternationalSignUpData = null,

                    NotificationEmail = result.Email,
                    SignupAccountData = new SignupAccountData
                    {
                        CurrencyCode = "USD",
                        Tier = "Test"
                    },
                    BusinessData =
                        new BusinessData
                        {
                            BusinessLegalName = result.BusinessLegalName,
                            DoingBusinessAs = result.DoingBusinessAs,
                            EIN = result.Ein,
                            MerchantCategoryCode = result.MerchantCategoryCode,
                            WebsiteURL = result.BusinessUrl,
                            BusinessDescription = result.BusinessDescription,
                            MonthlyBankCardVolume = result.MonthlyBankCardVolume ?? 0,
                            AverageTicket = result.AverageTicket ?? 0,
                            HighestTicket = result.HighestTicket ?? 0
                        },
                    Address = new Address
                    {
                        ApartmentNumber = result.Address1ApartmentNumber,
                        Address1 = result.Address1Line1,
                        Address2 = result.Address1Line1,
                        City = result.Address1City,
                        State = result.Address1State,
                        Country = result.Address1Country,
                        Zip = result.Address1ZipCode
                    },
                    MailAddress = new Address
                    {
                        ApartmentNumber = result.OwnerApartmentNumber,
                        Address1 = result.OwnerAddress,
                        Address2 = result.OwnerAddress2,
                        City = result.OwnerCity,
                        State = result.OwnerRegion,
                        Country = result.OwnerCountry,
                        Zip = result.OwnerZipCode
                    },
                    BusinessAddress =
                        new Address
                        {
                            ApartmentNumber = result.BusinessApartmentNumber,
                            Address1 = result.BusinessAddressLine1,
                            Address2 = result.BusinessAddressLine2,
                            City = result.BusinessCity,
                            State = result.BusinessState,
                            Country = result.BusinessCountry,
                            Zip = result.BusinessZipCode
                        },
                    BankAccount =
                        new BankAccount
                        {
                            AccountCountryCode = result.BankAccount1CountryCode,
                            BankAccountNumber = result.BankAccount1Number,
                            RoutingNumber = result.BankAccount1RoutingNumber,
                            AccountOwnershipType = result.BankAccount1OwnershipType,
                            BankName = result.BankAccount1BankName,
                            AccountType = "Checking",
                            AccountName = result.BankAccount1Name,
                            Description = result.BankAccount1Description
                        },
                    BeneficialOwnerData = new BeneficialOwnerData
                    {
                        OwnerCount = "1",
                        Owners = owner1
                    }
                };                    Console.WriteLine(JsonConvert.SerializeObject(signupRequest));

            }
        }
        return new SignUpRequest();
    }

我正在执行以下请求:

private static T Execute<T>(IRestRequest request, string baseUrl) where T : class, new()
    {

        var client = new RestClient(baseUrl);
        var response = client.Execute<T>(request);

        if (response.ErrorException != null)
        {
            Console.WriteLine(
                "Error: Exception: {0}, Message: {1}, Headers: {2}, Content: {3}, Status Code: {4}",
                response.ErrorException,
                response.ErrorMessage,
                response.Headers,
                response.Content,
                response.StatusCode);
        }

        Console.WriteLine("Status:" + response.StatusCode);
        Console.WriteLine("Message: " + response.Content);
        Console.WriteLine(response.Data);
        return response.Data;
    }

    public ProPayResponse MerchantSignUpForProPay()
    {
        var baseUrl = "https://xmltestapi.propay.com/ProPayAPI";
        var request = BuildMerchantTestData();
        var restRequest = CreateRestRequest("SignUp", Method.PUT);
        restRequest.AddJsonBody(request);
        _context?.SaveChangesAsync();
        return Execute<ProPayResponse>(restRequest, baseUrl);

    }

为什么JSON字符串在我的控制台中包含有效值,但API仍未接收到任何值?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

因此,在稍后再次访问此问题之后,我发现return signupRequest;是返回请求和请求值的适当方法。