Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Last active October 28, 2021 06:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imjasonh/8f79339d6df243fd4e31 to your computer and use it in GitHub Desktop.
Save imjasonh/8f79339d6df243fd4e31 to your computer and use it in GitHub Desktop.
Go script to ping the USPS package tracking API
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)
}
}
@martiscore
Copy link

Ministry of Finance has proposed several legislative changes that will affect lovers of orders from abroad. Be carefully

@brodi333
Copy link

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