简体   繁体   English

如何在 Cypress 中使用 JavaScript 在 GET API 的请求正文中发送 XML。 以 JSON 格式发送请求会给出响应,因为输入格式不正确

[英]How to send XML in request body of a GET API using JavaScript in Cypress. Sending request in JSON format gives response as Input was not well formed

I am hitting an API and this API returns XML response.我正在点击 API,这个 API 返回 XML 响应。 JavaScript code in Cypress is as follow - Cypress 中的 JavaScript 代码如下 -

        describe('Loan Details', ()=>{
     
       it('Get loan details', ()=>{
         cy.request({
         method : 'GET',
         url    : '/services/getLoans/getLoans.aspx',
         headers : {
             'api_user_id' : 'abc',
             'api_password': 'def',
             'loan_id'     : '8411'
         }
     }).then((res)=>{
        expect(res.status).to.eq(200)
        let myxml = Cypress.$.parseXML(res.body)
       cy.log(res.body)
       }) 
     }) 
    })

When I execute this code I am getting following output -当我执行此代码时,我得到以下 output -

 assert  expected 200 to equal 200
 log   <OUTPUT version="2.1"><ERROR type="NOT_WELL_FORMED">Input was not well formed and xml compliant.</ERROR></OUTPUT>

Therefore I am getting desired response that is 200 (success) from API but in order to validate loan data like loan_type, loan_number for a particular loan_id etc. I am pasting sample API response which we are getting after hitting the GET API.因此,我从 API 得到所需的响应,即 200(成功),但为了验证贷款数据,如 loan_type、特定 loan_id 的 loan_number 等。我正在粘贴样本 API 响应,这是我们在点击 GET API 后得到的。

   <OUTPUT version="2.0">
    <RESPONSE>
    <LOAN_DATA 
        loan_id="8411" 
        loan_number="61036" 
        loan_type="VL" 
        status_code="OK">
        <![CDATA[<VEHICLE_LOAN xmlns="abc.com" version="5.259"><APPLICANTS><APPLICANT paper_grade="" is_declined="N" first_name="CO APPLICANT F" last_name="TESTCASE" m_initial="" middle_name="" ssn="000000001" dob="1990-01-01" ages_of_dependents="" mother_maiden_name="" citizenship="USCITIZEN" member_number="" bankruptcy_score="0" membership_months="0" marital_status="MARRIED" number_in_household="1" credit_score="745" custom_score="0" custom_score_2="670" custom_score_3="690" custom_score_4="670" custom_score_5="670" applicant_type="P" allow_direct_mail="Y" allow_email_solicit="Y" allow_telemarket="Y" allow_share_info="Y" is_ofac_id_ver="N" id_authentication_result="INCOMPLETE" is_fraud_prescore="N" contact_index="0" d3p1:internal_borrower_id="5911" is_fraud_credit="N" is_ofac_credit="N" combined_unsecured_aggregate_amount_with_lender="0.00" combined_secured_aggregate_amount_with_lender_no_mortgage="0.00" borrower_id="0" xmlns:d3p1="abc.com"><ID_CARD card_type="SOCIAL_SECURITY" card_number="00-000-0006" state="" country="USA" /><ID_CARD2 card_type="" card_number="" state="" country="" /><CURRENT_ADDRESS occupancy_status="GOVQUARTERS" occupancy_duration="12" other_occupancy_description=""><LOOSE_ADDRESS street_address_1="123, CO APPLCIANT ADDRESS" street_address_2="" city="JACKSONVILLE" state="FL" zip="32259" county="" address_verification_date="2020-06-02" /></CURRENT_ADDRESS><PREVIOUS_ADDRESS occupancy_status="GOVQUARTERS" occupancy_duration="13" other_occupancy_description="" city="JACKSONVILLE" state="FL" zip="32259" county="" street_address_1="123, joint applciant address" street_address_2="" country="USA" /><MAILING_ADDRESS city="JACKSONVILLE" state="FL" zip="32259" county="" street_address_1="123, CO APPLCIANT ADDRESS" street_address_2="" country="USA" address_verification_date="2020-06-02" is_current="Y" /><FINANCIAL_INFO><CURRENT_EMPLOYMENT employment_start_date="2020-01-01" employed_months="26" employment_address="employment Address" employment_city="" employment_state="" occupation="Test Professional Job" employer="MERDIANLINK" employment_phone="" employment_status="EM" employment_weekly_hours="0" is_employee_of_lender="Y" employee_of_lender_type="EMPLOYEE" profession_months="26" /><PREVIOUS_EMPLOYMENT monthly_income="1000.00" employed_months="0" employment_address="EMP Addrtess" employment_city="" employment_state="" occupation="" employer="" employment_phone="" employment_weekly_hours="0" /><MONTHLY_INCOME is_monthly_income_base_salary_manual="N" is_bah_included="N" monthly_income_base_salary="5000.00" is_tax_exempt_monthly_income_base_salary="Y" monthly_income_over_time="0.00" monthly_income_bonus="0.00" income_verify_method="" /><MONTHLY_DEBT monthly_liability="1389.00" monthly_housing_cost="100.00" monthly_rent="100.00" job_expense="100.00" alimony="0.00" child_support="150.00" separate_maintenance_expense="0.00" tax_expense="0.00" other_expense_1="0.00" other_expense_2="0.00" monthly_mortgage_payment="0.00" aggregate_amount_with_lender="0.00" aggregate_amount_with_lender_secured_no_mortgage="0.00" /></FINANCIAL_INFO><REFERENCE first_name="" last_name="" address="" city="" state="" phone="" relationship="" email="" /><REFERENCE first_name="" last_name="" address="" city="" state="" phone="" relationship="" email="" /><CONTACT_INFO preferred_contact_method="HOME" cell_phone="" cell_phone_country="US" fax_number="" fax_number_country="US" home_phone="(999) 888-7776" home_phone_country="US" work_phone="" work_phone_extension="" work_phone_country="US" email="COAPPL@def.COM" /><ASSETS /><DECLARATIONS has_judge_bankrupt_foreclosure="" has_declare_bankrupt="" has_chapter_13="" has_lawsuit_party="" has_judgement="" has_foreclosure="" has_reposession="" has_other_obligation="" has_past_due_bills="" has_co_maker="" co_maker_amount="0.00" has_alias="" has_income_decline="" has_suits_pending="" has_alimony="" has_property_for_sale="" /></APPLICANT></APPLICANTS><LOAN_INFO rate_code="" payment_due_day="0" max_underwrite_amount="0" epl_fees_total="0.00" is_claimed="Y" is_complete_consumer="N" require_product_refresh="N" is_addon="N" is_amendment="N" solve_for="PAYMENT" override_code="04" is_possible_dupe="N" app_subtype="DVL" is_workout_loan="N" is_solvefor_manual="N" tier="0" tier_previous="0" is_prequalification_applied="N" is_ofac="N" is_indirect_loan="N" is_business="N" total_sales_price="0.00" total_vehicle_value="2000.00" amount_requested="1000.00" down_payment="0.00" loan_term="36" contract_rate="0.00000" trade_allowance="0.00" initial_balloon_amount="0.00" estimated_vehicle_payment="0.00" is_residual_loan="N" rebate="0.00" tax="0.00" license="0.00" additional_items_front_end="0.00" additional_items_back_end="0.00" max_advance="" max_advance_value="0.00" max_soft_advance_value="0.00" max_hard_advance_value="0.00" amount_approved="0.00" request_type="AUTO SECURED" purpose_type="AUTO SECURED" inspected_by="" misc_implant="" number_of_payments="0" is_balloon="N" is_hmda="N" is_hmda_manual="N" amount_approved_variance="0.00" is_advance_request="N" is_calculate_mapr="Y" mapr="0.00000" customer_money_factor="0" lender_money_factor="0" residual_value="0.00" gross_cap_cost="0.00" cap_cost_reduction="0.00" adjusted_cap_cost="0.00" lease_payment="0.00" max_lease_payment="0.00" security_deposit="0.00" net_due_to_dealer="0.00" /><PRICE_ADJUSTMENTS /><LOAN_STATUS loan_status="APP" approval_date="2022-03-02T02:00:57-08:00" first_approval_date="2022-03-02T01:59:34-08:00" response_date="2022-03-02T01:59:34-08:00" initial_entry_timestamp="2022-03-02T01:46:32-08:00" last_modified="2022-03-02T02:04:03-08:00" is_locked="N" existing_balance="0.00" delinquency_period="0" delinquency_amount="0.00" deciding_date="2022-03-02" app_receive_date="2022-03-02T01:46:32-08:00" amount_charge_off="0.00" /><DISBURSEMENTS /><TASK_LIST /><FUNDING daily_periodic_rate="0.00000" payment_frequency="MONTHLY" funded_status="FUN" funding_date="2022-03-02" amount_advanced="900.00" current_payment="0.00" filing_fee_amount="0.00" filing_fee_type="FINANCED" finance_charge="0.00" initial_amount_advanced="900.00" other_charges="0.00" other_charges_description="" other_loans="0.00" prev_loan_liner_balance="0.00" total_financed_payment="900.00" interest_only_min_payment="0.00" interest_only_max_payment="0.00" loan_term="0" reserve_amount="0.00" primary_reserve_amount="0.00" secondary_reserve_amount="0.00" suggested_buydown_amount="0.00" extended_rate="0.00000" buy_rate="0.00000" contract_rate="0.00000" index="0.00000" margin="0.00000" floor="0.00000" ceiling="0.00000" apr="0.00000" exact_monthly_payment="120.00" exact_monthly_payment_original="0.00" exact_last_monthly_payment="0.00" additional_fees="0.00" additional_fees_comment="" funding_acquisition_fee="0.00" initial_rate="-999999.00000" doc_stamps_fee="0.00" doc_stamps_rate="0.00000" doc_stamps_fee_is_manual="N" doc_stamps_type="FINANCED" funding_money_factor="0" funding_equivalent_interest_rate="0.00000" funding_allowable_annual_mileage="0"><SKIP_PAYMENT_INFO skip_start_date="0001-01-01" skip_end_date="0001-01-01" skip_period_month_day_start="0001-01-01" skip_period_month_day_end="0001-01-01" skips_per_year="0" position_of_skips="0" is_position_of_skips_first_pay_skip="N" is_defer_skip_payments="N" /><INSURANCE_OPTIONS loan_class="C" description="" is_mapr_gap="Y" is_mapr_sc="N" is_mapr_vpp="N"><INSURANCE_OPTION type="GAP" premium="0.00" is_financed="Y" /><INSURANCE_OPTION type="SERVICE_CONTRACT" premium="0.00" is_financed="Y" /><INSURANCE_OPTION type="VPP" premium="0.00" is_financed="Y" /></INSURANCE_OPTIONS><CLOSE_END_INFO amount_owed_to_lender="900.00" amount_refinanced="0.00" cash_prepaid_finance_charge="0.00" mapr_cash_fee="0.00" /></FUNDING><COMMENTS><DECISION_COMMENTS>Kumar, Amit (3/2/2022 5:00:57 AM EST): DONT GO THERE
        </DECISION_COMMENTS><EXTERNAL_COMMENTS></EXTERNAL_COMMENTS><INTERNAL_COMMENTS></INTERNAL_COMMENTS><UNABLE_TO_FUND></UNABLE_TO_FUND></COMMENTS><STRUCTURED_COMMENTS><STRUCTURED_INTERNAL_COMMENTS><STRUCTURED_COMMENT modifier_type="Z" last_modified_date="2022-03-02T01:48:54.0000000-08:00" creation_date="2022-03-02T01:48:54.0000000-08:00" message="Applicant (TESTCASE, CO APPLICANT F) has a record in the Applicant Designation.  The applicant's Employee Of Lender Type is set to: EMPLOYEE"><MODIFIER d5p1:internal_id="00000000000000000000000000000001" xmlns:d5p1="abc.com" /></STRUCTURED_COMMENT><STRUCTURED_COMMENT modifier_type="Z" last_modified_date="2022-03-02T01:59:56.0000000-08:00" creation_date="2022-03-02T01:59:56.0000000-08:00" message="Switched to pending status because amount approved has increase from 0 to 900"><MODIFIER d5p1:internal_id="00000000000000000000000000000001" xmlns:d5p1="abc.com" /></STRUCTURED_COMMENT></STRUCTURED_INTERNAL_COMMENTS></STRUCTURED_COMMENTS><VEHICLES><VEHICLE mileage="5" is_new_vehicle="Y" vehicle_sales_price="0.00" vehicle_value="2000.00" is_rebuilt_restored="N" processing_type="CT" vehicle_options="SUN ROOF - FLIP" vin="" vehicle_type="CAR" year="2022" make="BENTLEY" model="test" license="" nada_info="" body_type="" empty_weight="0" carrying_capacity="0" length="0" commercial_title="NONE"><CLEAR_TITLE current_name_on_title="TESTCASE, CO APPLICANT F" new_names_on_title="TESTCASE, CO APPLICANT F" name_for_collateral_purposes="TESTCASE, CO APPLICANT F" title_recording_fees="0.00" current_name_on_title_contact_index="0" new_names_on_title_contact_index="0" name_for_collateral_purposes_contact_index="0" /><TITLE state_current="" /><INSURANCE /></VEHICLE><TRADE_IN trade_value="0.00" trade_payoff="0.00" trade_payment="0.00" vin="" vehicle_type="CAR" year="0" make="" model="" /></VEHICLES><CUSTOM_QUESTIONS /><CONTACTS><CONTACT_INFO contact_type="APPLICANT" first_name="CO APPLICANT F" last_name="TESTCASE" address="123, CO APPLCIANT ADDRESS" city="JACKSONVILLE" state="FL" zip="32259" phone="(999) 888-7776" email="COAPPL@def.COM" ssn="000000001" is_ssn_taxid="N" dob="1990-01-01" /></CONTACTS><SYSTEM type="LPQ" d2p1:loan_id="892fb4" xmlns:d2p1="abc.com"><PROCESSOR d2p1:internal_id="4cc78ffcca0d" name="test" phone="(989) 898-9800" code="TEST_301" email="test@abc.com" /><LOAN_OFFICER d2p1:phone="(989) 898-9898" code="TEST_301" email="test@abc.com" /><APPROVAL_OFFICER d2p1:phone="(989) 898-9898" code="TEST_301" email="test@abc.com" /><FUNDER d2p1:phone="(989) 898-9898" code="TEST_301" email="test@abc.com" /><DECIDING_OFFICER d2p1:phone="(989) 898-9898" code="TEST_301" email="test@abc.com" /><ORIGINATOR d2p1:internal_id="4cc78ffcca0" name="test" phone="(989) 898-9898" code="TEST_301" email="test@abc.com" type="L" /><BRANCH d2p1:internal_id="0c845c56bf89e232e" name="Branch1" code="BRANCH1_217" phone="714-651-5553" fax="324-234-2342,123-456-7899, 123-456-6565" email="dvu@abc.com" reference_id2="2"><ADDRESS street_address_1="1600 Sunflower Ave" city="Costa Mesa" state="CA" zip="92626" /></BRANCH><LENDER d2p1:internal_id="cb8f7cda8a6" name="Lender Hive" reference_id="UnitTestReference" code="PleaseChangeMe_6688a99921444657ba0c318471f05ecd" phone="714-333-2988" fax="714-333-2988" email="ben@abc.com" cuna_client_id="10416516"><ADDRESS street_address_1="144327 Corky St." city="Westminster" state="IL" zip="29375" /></LENDER><ORGANIZATION d2p1:internal_id="7fcd0b9452ca46a7a194bc3f2324905e" name="QA-Hive" reference_id="UnitTestOrgReference" code="QA-Hive" phone="(714) 333-2988" fax="(714) 333-2988" /><CLOSING_BRANCH d2p1:internal_id="0c84532e" name="Branch1" reference_id="1" code="BRANCH1_217" phone="714-651-5553" fax="324-234-2342,123-456-7899, 123-456-6565" email="dvu@abc.com" reference_id2="2"><ADDRESS street_address_1="1600 Sunflower Ave" city="Costa Mesa" state="CA" zip="92626" /></CLOSING_BRANCH></SYSTEM><HMDA_INFO is_hoepa="N" is_hoepa_manual="N" lien_position="1" purchaser="NOT_ORIGINATED" /><HMDA_XML version="2018" xmlns="abc.com"></HMDA_XML><VEHICLE_BUSINESS_INFORMATIONS number_of_employees="0" months_in_business="0" is_borrower_prepared="N" is_cpa_prepared="N" is_income_tax_statements_available="N" mailing_address_is_current="Y" mailing_country="USA" occupancy_duration="0" /></VEHICLE_LOAN>]]>
     </LOAN_DATA>
   </RESPONSE>
  </OUTPUT>

Just got to know that input to this should only be in XML format.刚刚知道对此的输入只能是 XML 格式。 I am adding input XML as well here for better understanding.为了更好地理解,我在这里也添加了输入 XML。

      <INPUT>
           <LOGIN api_user_id="f782481b" api_password="Sn63P73P"/>
            <REQUEST>
               <LOAN loan_id="d023bcf"/>
            </REQUEST>
      </INPUT>

Please help me get this resolved.请帮我解决这个问题。 I was thinking to store the response in a variable/constant and then apply some methods but it did not work.我正在考虑将响应存储在变量/常量中,然后应用一些方法,但它没有用。

I tried multiple ways to pass request XML format and finally able to get desired response.我尝试了多种方式来传递请求 XML 格式,最终能够得到想要的响应。 Please see below the working code -请参阅下面的工作代码 -

   describe('Loan Details', () => {
     it('Info', () => {
     cy.readFile('D:/Cypress/CypressPractice/myRequest.xml')
      .then(text => fetchXML(text))
      .then(response =>{
          expect(response.status).to.eq(200)
          cy.log(response.body)
      })
          
   function fetchXML(text){
       return cy.request({
        method: 'GET',
        url: '/services/getLoans/getLoans.aspx',
        body : text
       })
     }
   })
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM