Reputation: 1
I'm using USPS NEW API(https://developer.usps.com/api/71) to create domestic label, the response is not standard JSON/XML,it's some data with headers(I don't know what's the format is),see below
--z-zSV9BXZtRNip-8GHCvJoED
Content-Type: application/json
Content-Disposition: form-data; name="labelMetadata"
{"labelAddress":{"streetAddress":"2512 3N","secondaryAddress":"","city":"APPLE","state":"WI","ZIPCode":"54913","firstName":"Dan","lastName":"Run","ignoreBadAddress":true},"routingInformation":"42054913","trackingNumber":"9200190359099201038375","postage":0.00,"extraServices":[{"name":"USPS Tracking","price":0.0,"SKU":"DXTU0EXXXCX0000"}],"zone":"05","commitment":{"name":"3 Days","scheduleDeliveryDate":"2024-09-27"},"weightUOM":"LB","weight":0.22,"dimensionalWeight":0.0,"fees":[],"bannerText":"USPS TRACKING # USPS Ship","constructCode":"C03","SKU":"DUXP0XXXUC05040"}
--z-zSV9BXZtRNip-8GHCvJoED
Content-Type: application/pdf
Content-Disposition: form-data; filename="labelImage.pdf"; name="labelImage"
JVBERi0xLjQKJaqrrK0KMSAwIG9iago8PAovUHJvZHVjZXIgKEFwYWNoZSBGT1AgVmVyc2lvbiBTVk46IFBERiBUcmFuc2NvZGVyIGZvciBCYXRpaykKL0NyZWF0aW9uRGF0ZSAoRDoyMDI0MDkyNDA3NTg1OFopCj4+CmVuZG9iagoyIDAgb2JqCjw8CiAgL04gMwogIC9MZW5ndGggMyAwIFIKICAvRmlsdGVyIC9GbGF0ZURlY29kZQo.......
--z-zSV9BXZtRNip-8GHCvJoED--
You can tell the data before '--z-zSV9BXZtRNip-8GHCvJoED' is a header with tracking info(JSON data), and after '--z-zSV9BXZtRNip-8GHCvJoED' is a header with PDF(base64)
Any one can know what the data format is, and how to deserialize it?
I need a way to deserialize this kind of data like normally we do to deserialize JSON/XML data
Upvotes: 0
Views: 169
Reputation: 2401
Short answer: Make your request with an Accept: application/vnd.usps.labels+json
header and the API will return a JSON payload to you without the multi-part nonsense.
Notice that today's documentation has the wrong schema for the JSON response. I did hear back from USPS' developer support and they will be correcting that documentation in the future. (The documentation shows members under a labelMetaData
object when in actuality they are returned as members of the top-level document.)
POST https://api.usps.com/labels/v3/label HTTP/1.1
Accept: application/vnd.usps.labels+json
Authorization: Bearer [redacted]
Content-Length: 1086
Content-Type: application/json
X-Payment-Authorization-Token: [redacted]
{
"imageInfo" : {
"imageType" : "ZPL203DPI",
"labelType" : "4X6LABEL",
"receiptOption" : "NONE",
"suppressPostage" : true,
"suppressMailDate" : true,
"returnLabel" : false
},
"toAddress" : {
...
},
"fromAddress" : {
...
},
"packageDescription" : {
"weight" : 0.5,
"length" : 5.0,
"width" : 5.0,
"height" : 4.0,
"mailClass" : "PRIORITY_MAIL",
"processingCategory" : "MACHINABLE",
"destinationEntryFacilityType" : "NONE",
"extraServices" : [ 920 ],
"mailingDate" : "2024-12-16",
"rateIndicator" : "SP"
}
}
{
"labelImage": "<base64>",
"labelAddress": {
...
},
"routingInformation": "...",
"trackingNumber": "...",
"postage": 6.17,
"extraServices": [
{
"name": "USPS Tracking",
"price": 0,
"SKU": "DXTP0EXXXCX0000"
}
],
"zone": "02",
"commitment": {
"name": "2 Days",
"scheduleDeliveryDate": "2024-12-19"
},
"weightUOM": "LB",
"weight": 0.5,
"dimensionalWeight": 0,
"fees": [],
"bannerText": "USPS TRACKING # USPS Ship",
"retailDistributionCode": "03",
"serviceTypeCode": "055",
"constructCode": "C03",
"SKU": "DPXX0XXXX102005"
}
Upvotes: 0
Reputation: 560
The default media type of the response has multiple parts. Setting the Accept header to either application/json or application/xml will only affect the metadata part of the multipart response.
try to add ["Accept":"application/json"] to your request header
Upvotes: 0