Skip to content Skip to sidebar Skip to footer

Python - Send A Request For Each Item In A List With Dynamic Values

I'm currently working with the UPS Package Tracking API to send a tracking number via a POST request that the API receives and sends back a JSON response with information about the

Solution 1:

You are reusing trk variable for different things (for a list and then for a single InquiryNumber) in your example.

import requests


nme = 'your_username'
pwd = 'your_password'
aln = 'your_accesslicensenumber'

tracking_numbers = ["tracknbr1", "tracknbr2", "tracknbr3"]

for trk in tracking_numbers:
    data = {"UPSSecurity" : {
            "UsernameToken" : {
                "Username" : nme,
                "Password" : pwd
            },
            "ServiceAccessToken" : {
                "AccessLicenseNumber" : aln
            },
        },
        "TrackRequest" : {
            "Request" : {
                "RequestOption" : 1,
                "TransactionReference" : {
                    "CustomerContext" : ""
                },
            },
            "InquiryNumber" : trk
        }
        }

    response = requests.post("https://wwwcie.ups.com/rest/Track", json=data)
    print(response.json())

Post a Comment for "Python - Send A Request For Each Item In A List With Dynamic Values"