Search code examples
pythontextpython-requests

Passing data from a text file to python request only passes the last line


I am trying to pass as single column text file data to a python request.

Specifically I tried to replace the original data request that looked like this

data ='{person:123456}'

with the open file below. The text file structure looks like this:

{person:15590665}
{person:15590666}
{person:15590667}
{person:15590668}

The problem is that no matter what I do , it only reads the last one ({person:15590668}) ignoring all previous ones, the response is correct for this particular one.

This is the request:

import requests
import json
cookies = {
    'cookieconsent_status': 'deny',
    'AWSALB': '**************************************************/Q==',
    'AWSALBCORS': '*********************************************==',
    'ctfloginPha': '******************************************************n',
}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
    'Accept': 'application/json',
    'Accept-Language': 'en-US,en;q=0.5',
    # 'Accept-Encoding': 'gzip, deflate, br',
    'Content-Type': 'application/json; charset=utf-8',
    'Authorization': '**************************==',
    'X-Requested-With': 'XMLHttpRequest',
    'Origin': 'https://**************',
    'Connection': 'keep-alive',
    'Referer': 'https://********************',
    # Requests sorts cookies= alphabetically
    # 'Cookie': 'cookieconsent_status=deny; AWSALB=***********************************************',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    # Requests doesn't support trailers
    # 'TE': 'trailers',
}

with open('person.txt') as fl:
   for line in fl:
      data =line


response = requests.post(
    'https://***************************************',
    cookies=cookies,
    headers=headers,
    data=data,
)
print(response.json())

Solution

  • Just move your code inside the for loop otherwise you send the request after the loop has finished and data contains the last line.

    import requests
    import json
    cookies = {
        'cookieconsent_status': 'deny',
        'AWSALB': '**************************************************/Q==',
        'AWSALBCORS': '*********************************************==',
        'ctfloginPha': '******************************************************n',
    }
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
        'Accept': 'application/json',
        'Accept-Language': 'en-US,en;q=0.5',
        # 'Accept-Encoding': 'gzip, deflate, br',
        'Content-Type': 'application/json; charset=utf-8',
        'Authorization': '**************************==',
        'X-Requested-With': 'XMLHttpRequest',
        'Origin': 'https://**************',
        'Connection': 'keep-alive',
        'Referer': 'https://********************',
        # Requests sorts cookies= alphabetically
        # 'Cookie': 'cookieconsent_status=deny; AWSALB=***********************************************',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        # Requests doesn't support trailers
        # 'TE': 'trailers',
    }
    
    with open('person.txt') as fl:
       for line in fl:
            response = requests.post(
                'https://***************************************',
                cookies=cookies,
                headers=headers,
                data=line,
            )
            print(response.json())