【问题标题】:Add dynamic data in XML body for AXIOS Post在 AXIOS Post 的 XML 正文中添加动态数据
【发布时间】:2020-10-01 01:58:42
【问题描述】:

我需要使用 AXIOS 发布请求。请求正文为 XML 格式。我可以使用 AXIOS 在 XML 正文中使用静态数据发布请求,但希望动态传递值。

您能否告诉我如何在 xml 正文中添加动态值(TripName、TotalFare 等)?

requestBody='<Itinerary xmlns="http://www.testsol.com/api/travel/trip/2010/06">\
    <TripName>SFO Trip- air and hotel </TripName>\
    <Comments />\
    <StartDateLocal>2020-05-10T07:25:00</StartDateLocal>\
    <EndDateLocal>2020-05-14T23:59:00</EndDateLocal>\
    <Bookings>\          
        <Booking>\
               <AirlineTickets>\
                <AirlineTicket>\
                    <DateCreatedUtc>2020-05-11T07:34:13</DateCreatedUtc>\
                    <DateModifiedUtc>2020-05-13T10:52:27</DateModifiedUtc>\
                    <IssueDateTime>2020-05-11T00:34:13</IssueDateTime>\
                    <TotalFare>3948.0000</TotalFare>\
                    <TotalFareCurrency>INR</TotalFareCurrency>\
                    <AirlineTicketCoupons>\
                        <AirlineTicketCoupon>\                            
                            <EndCityCode>DEL</EndCityCode>\
                            <FlightNumber>198</FlightNumber>\
                            <StartCityCode>BLR</StartCityCode>\
                            <StartDateLocal>2020-03-19T20:30:00</StartDateLocal>\
                            <Vendor>SG</Vendor>\
                        </AirlineTicketCoupon>\
                    </AirlineTicketCoupons>\
                </AirlineTicket>\
            </AirlineTickets>\               
            </Passengers>\            
            <PassengerCount>1</PassengerCount>\
        </Booking>\
    </Bookings>\
</Itinerary>';

 const config_req = {
      headers: {
        //  Accept: "application/json",
        Authorization: "Bearer " + token,
      },
    };

代码片段

 axios
      .post("https://test.com/api/travel/trip", requestBody, config_req)
      .then((result) => {
        console.log("create Itin API" + result.data);
      })
      .catch((error) => {
        console.log(error);
        console.log(error.data);
      });

【问题讨论】:

    标签: reactjs react-native react-redux react-router axios


    【解决方案1】:

    您可以使用template strings 作为您的请求正文。

    例如

    const startDateLocal = ...
    const endDateTotal = ...
    const dateCreated = ...
    ...
    requestBody=`<Itinerary xmlns="http://www.testsol.com/api/travel/trip/2010/06">\
        <StartDateLocal>${startDateLocal}</StartDateLocal>\
        <EndDateLocal>${endDateTotal}</EndDateLocal>\
        <Bookings>\          
            <Booking>\
                   <AirlineTickets>\
                    <AirlineTicket>\
                        <DateCreatedUtc>${dateCreated}</DateCreatedUtc>\
                        <DateModifiedUtc>${dateModified}</DateModifiedUtc>\
                        <IssueDateTime>${issueDate}</IssueDateTime>\
                        <TotalFare>${totalFare}</TotalFare>\
                        <TotalFareCurrency>${currency}</TotalFareCurrency>\
                    </AirlineTicket>\
                </AirlineTickets>\               
                </Passengers>\            
                <PassengerCount>${passengerCount}</PassengerCount>\
            </Booking>\
        </Bookings>\
    </Itinerary>`;
    

    假设您已经定义了所有变量

    【讨论】:

    • @gzcz- 您分享的示例适用于单个标签。我需要更新 xml 中的多个值。我可以知道我该怎么做吗?
    • 您可以将整个请求正文设为模板字符串...const body = &lt;Ticket&gt;&lt;TotalFare&gt;${totalFare}&lt;/TotalFare&gt;&lt;IssueDateTime&gt;${date}&lt;/IssueDateTime&gt;&lt;/Ticket&gt;。更新了答案以向您展示更好的示例。
    猜你喜欢
    • 2021-07-02
    • 2022-06-29
    • 2022-01-27
    • 1970-01-01
    • 2019-10-11
    • 2018-05-17
    • 2019-02-09
    • 2023-04-04
    • 2014-07-15
    相关资源
    最近更新 更多