0

I'm trying to write a python script to get the tracking info from this page: https://www.purolator.com/en/app-tracker.page. Using F12 in Chrome, I got the header and the request payload, no form data just request payload. The tracking number I'm trying is: GRH000241377

Here's my code:

 header = {
        'authority': 'api.purolator.com',
        'method': 'POST',
        'path': '/tracker/puro/json/shipment/search',
        'scheme': 'https',
        'accept': 'application/vnd.puro.shipment.trackingevent+json',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-CA',
        'content-length': '90',
        'content-type': 'application/vnd.puro.shipment.trackingevent+json',
        'origin': 'https://www.purolator.com',
        'referer': 'https://www.purolator.com/en/app-tracker.page?pin=GRH000241377&sdate=2019-12-09',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/79.0.3945.117 Safari/537.36'
    }

    data = {
        'pins':'[{"pin":"GRH000241377","sequenceID":1}]',
        'searchOptions':'{"includeReference":true}'
    }


    url = "https://www.purolator.com/en/app-tracker.page"
    response = requests.post(url, headers=header, json=data)
    res_json = response.json()

Response code is 200 but when i print the response.text i get the same page without any result, and if try to get response.json() i got next error, I'm assuming that error is because i have no json in the response

'Error occurred : \n Error Message: Expecting value: line 1 column 1 (char 0)'

Presumably error is on data or header construction but can't figure out the problem. Any idea? Thanks

Here are the response headers:

access-control-allow-origin: https://www.purolator.com
content-length: 1102
content-security-policy: default-src 'self' *.hs-cpggpc.ca *.canadapost.ca *.cpggpc.ca *.purolator.com *.epost.ca
content-type: application/vnd.puro.shipment+json
date: Tue, 14 Jan 2020 00:37:38 GMT
status: 200
strict-transport-security: max-age=86400; preload
svn-build-number: Purolator.buildLevel.20190622.BF_2019A-B15568.15568
via: 1.1 199fd61d7551d8868317c5b53cc7d24d.cloudfront.net (CloudFront)
x-amz-apigw-id: GQ8E3HOV4osFhyQ=
x-amz-cf-id: Yqyt1uuT9NuucZiYP7arFLAFnTdKbK8yAfRrJ5nfnV7kUyNbhXH0OQ==
x-amz-cf-pop: IAD89-C3
x-amzn-remapped-connection: keep-alive
x-amzn-remapped-content-length: 1102
x-amzn-remapped-date: Tue, 14 Jan 2020 00:37:38 GMT
x-amzn-requestid: a04f8174-fc03-49f5-864c-23bb76ff4434
x-backside-transport: OK OK,OK OK,OK OK,OK OK
x-cache: Miss from cloudfront
x-content-type-options: nosniff
x-frame-options: ALLOW-FROM https://https://www.purolator.com
x-xss-protection: 1; mode=block
3
  • 1
    Could you please attach the response header as well? Commented Jan 14, 2020 at 1:09
  • A POST request doesn't have to return anything, that's what GET is for Commented Jan 14, 2020 at 1:13
  • What's in response.content ? Commented Jan 14, 2020 at 15:57

1 Answer 1

1

I hope this helps.

Python code:

import requests

header = {
  'authority': 'api.purolator.com',
  'method': 'POST',
  'path': '/tracker/puro/json/shipment/search',
  'scheme': 'https',
  'accept': 'application/vnd.puro.shipment.trackingevent+json',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'en-CA',
  'content-length': '90',
  'content-type': 'application/vnd.puro.shipment.trackingevent+json',
  'origin': 'https://www.purolator.com',
  'referer': 'https://www.purolator.com/en/app-tracker.page?pin=GRH000241377&sdate=2019-12-09',
  'sec-fetch-mode': 'cors',
  'sec-fetch-site': 'same-site',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
}

data = {"trackingNumbers":[{"trackingNumber":"GRH000241377", "type":"Unspecified","sequenceID":1}]}

url = "https://api.purolator.com/tracker/puro/json/shipment/trackingEvent/summary/search"
response = requests.post(url, headers=header, json=data)
res_json = response.json()

print (res_json)

Output:

{"trackingResults": [{"searchStatus": "Found", "foundBy": "Pin", "searchCriteria": {"trackingNumber": "GRH000241377", "type": "Unspecified", "sequenceID": 1}, ..., "isStateBehind": False}
Sign up to request clarification or add additional context in comments.

4 Comments

please expound and explain what the problem is and how you solve it. Don't expect people to do a diff on the code
This URL "purolator.com/en/app-tracker.page" provides shipment tracking. I enter this URL in Google Chrome and hit enter, then click Develop Tools and go to Network tab for tracing network request sources. I type PIN into the input field, and click "Track Now" button. In Network tab, I can see all network activity in the Network Log, then I found this URL "api.purolator.com/tracker/puro/json/shipment/trackingEvent/…", which returns tracking information.
Thanks so much, it works, but i need a little bit more info than is not shown in the response, im trying to use this payload:data = {"pins":[{"pin":"GRH000241377", "sequenceID":1}], "searchOptions":{"includeReference":True}} but the response is: [{'code': 'E019', 'description': "[JSV0001] Invalid value type 'array'."}]
Thanks Neda Peyrone finally solved, problem was on url and headers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.