I am trying to setup UPS Rate API for one of my app, my data looks like:
{
"RateRequest": {
"Request": {
"RequestOption": "Rate"
},
"CustomerClassification": {
"Code": "00"
},
"Shipment": {
"Shipper": {
"Name": "**********",
"ShipperNumber": "*****",
"Address": {
"AddressLine": "7550 N Croname RD STE 500",
"City": "Niles",
"StateProvinceCode": "IL",
"PostalCode": "*****",
"CountryCode": "US"
}
},
"ShipTo": {
"Address": {
"PostalCode": "90001",
"CountryCode": "US"
}
},
"ShipFrom": {
"Address": {
"AddressLine": "******",
"City": "Los Angeles",
"StateProvinceCode": "*****",
"PostalCode": "*****",
"CountryCode": "US"
}
},
"Service": {
"Code": "03"
},
"TaxInformationIndicator": "True",
"NumOfPieces": "1",
"Package": [
{
"PackagingType": {
"Code": "02"
},
"PackageWeight": {
"Weight": "7.31",
"UnitOfMeasurement": {
"Code": "LBS"
}
}
}
],
"ShipmentRatingOptions": {
"NegotiatedRatesIndicator": "True"
}
}
}
}
I am targeting the URL https://onlinetools.ups.com/api/rating/v2409/Rate, but I am getting response something like:
{"response":{"errors":[{"code":"9110034","message":"Missing or invalid request option."}]}}
While the RequestOption
is available in the payload. What I am doing wrong? (I have access token).
my code for call is
$version = "v2409";
$requestoption = "Rate";
$accessToken = $this->getUpsAccessToken(); // Get OAuth token
if (!$accessToken) {
return ['error' => 'Failed to get access token'];
}
$query = array();
$curl = curl_init();
$payload = $this->processPackageRateRequest($ShippingConstants, $PostalCode, $CountryCode, $BoxCount, $ShippingWeight, $ServiceType, $addressType);
//print_r(json_encode($payload));
curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $accessToken",
"Content-Type: application/json",
"transId: string",
"transactionSrc: testing",
"Accept: application/json",
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_URL => "https://onlinetools.ups.com/api/rating/" . $version . "/" . $requestoption . "?" . http_build_query($query),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo "cURL Error #:" . $error;
} else {
echo $response;
}
the function "processPackageRateRequest" returns the payload in formatted version.
Update based on the comment
You are setting a random string
value to the additionalinfo
parameter. The only valid value listed in the API docs is timeintransit
. So either remove additionalinfo
parameter or set it's value to timeintransit
.
Original Answer
You have one required field missing in your data. For UnitOfMeasurement you need to provide both Code and Description. Check this.
Description | string [ 1 .. 35 ] characters <= 1 |
---|---|
required | Text description of the code representing the unit of measure associated with the package weight. Length and value are not validated. |
So add relevant text description to your data.
...
"PackageWeight": {
"Weight": "7.31",
"UnitOfMeasurement": {
"Code": "LBS",
// Set the relevant description here
"Description": "Pounds"
}
}
...