skip to Main Content

Trying to make a same request implemented in Code (1) using python, which is a part of a backend data fetching process on an online card game card lookup website (https://splintx.com/). The intended response should contain two data (date data, and price history data) as shown in (2).

How could I simulate this using python?
I tried it with (3) but the response only contains date data. It returns the price history data empty (see (4)).
Thanks.

(1) Code excerpt – Github

    componentDidUpdate(prevProps, prevState) {
        if (this.state.panel === 'forSale' && prevState.panel === 'multiSelect' && this.state.selected !== prevState.selected) {
            this.updateSort('selected');
        } else if (this.state.panel === 'history' && prevState.panel !== 'history') {
            let edition = this.props.info.edition === 'Alpha' ? 0 : this.props.info.edition === 'Beta' ? 1 : this.props.info.edition === 'Promo' ? 2 : this.props.info.edition === 'Reward' ? 3 : this.props.info.edition === 'Untamed' ? 4 : 5;
            let foilInt = this.props.info.gold ? 1 : 0;
            let id = this.props.info.detailID;
            let priceData = JSON.stringify([{
                id: id,
                foil: foilInt,
                edition: edition
            }]);
            let storageSearch = 'history-' + priceData;

            ...

                $.ajax({
                    type: 'POST',
                    url: 'https://splintx.com/db.php',
                    data: {card: priceData},
                    jsonpCallback: 'testing',
                    dataType: 'json',
                    success: function(history) {
                        let expiry = new Date();
                        expiry.setDate(expiry.getDate() + 1);
                        expiry.setUTCHours(0, 0, 0, 0);
                        let historyObj = {
                            expiry: expiry,
                            data: history
                        };
                        let storageName = 'history-' + priceData;
                        sessionStorage.setItem(storageName, JSON.stringify(historyObj));
                        let prices = history[1];
                        let dates = history[0];
                        let newPrices = [];
                        let newDates = [];
                        ...

(2) Intended response (on Chrome Devtools->Application->Session Storage)

enter image description here

(3) Python code

import requests
res = requests.get(
    url='https://splintx.com/db.php',
    params={
        'card':
            {
                'id': 141,
                'foil': 0,
                'edition': 4,
            }

    },
)

(4) Unintended response – response[1] is supposed to have price history data but is empty

[["2020-08-24 12:00:01am","2020-08-25 12:00:01am","2020-08-26 12:00:01am", ...], []]

2

Answers


  1. Chosen as BEST ANSWER

    After a further debugging, I found that the data: {card: priceData} is actually sent as Form Data, not as query string. (Differences between Form Data and query string)
    Form Data is sent inside the HTTP body of a POST request, and with the help of this, I have realised the below working code.

    Not quite sure why Session should be created upfront when dealing with Form Data yet.

    import requests
    
    pricedata = '[{"id":141,"foil":0,"edition":4}]'
    data = {'card': pricedata}
    
    session = requests.Session()
    req = session.post('https://splintx.com/db.php', data=data)
    
    print(req.content)
    

  2. Since you are JSON-encoding your Javascript, you need to JSON-encode your Python as well:

    import requests
    res = requests.get(
        url='https://splintx.com/db.php',
        params={
            'card': json.dumps(
                {
                    'id': 141,
                    'foil': 0,
                    'edition': 4,
                }
            )
        },
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search