Last active
October 28, 2021 06:22
-
-
Save imjasonh/8f79339d6df243fd4e31 to your computer and use it in GitHub Desktop.
Go script to ping the USPS package tracking API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/xml" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
) | |
var ( | |
user = flag.String("user", "", "USPS API User ID") | |
id = flag.String("package_id", "", "USPS Package ID") | |
) | |
type response struct { | |
Summary string `xml:"TrackInfo>TrackSummary"` | |
Detail []string `xml:"TrackInfo>TrackDetail"` | |
} | |
func main() { | |
flag.Parse() | |
url := "http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML=" + | |
url.QueryEscape(fmt.Sprintf(`<TrackRequest USERID="%s"><TrackID ID="%s"></TrackID></TrackRequest>`, *user, *id)) | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Fatalf("get: %v", err) | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != http.StatusOK { | |
all, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatalf("error reading response: %v", err) | |
} | |
log.Fatalf("HTTP error %d\n%s", resp.StatusCode, string(all)) | |
} | |
var r response | |
if err := xml.NewDecoder(resp.Body).Decode(&r); err != nil { | |
log.Fatalf("error decoding xml: %v", err) | |
} | |
log.Println("Summary", r.Summary) | |
log.Println("Details:") | |
for _, d := range r.Detail { | |
log.Println("-", d) | |
} | |
} |
Are you used to buying products from abroad? Well, you have to be careful because the authorities intend to monitor their value. Specifically, the Ministry of Finance has proposed several legislative changes that will affect lovers of orders from abroad. Whenever I order, I think the package is checked at customs, and they look at what is in it. And to see where the package is and where something of it could be lost, I use newgistics parcel tracking, which shows me the current location where the package is.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ministry of Finance has proposed several legislative changes that will affect lovers of orders from abroad. Be carefully