Calling the USPS API

20 views
Skip to first unread message

J-vibe

unread,
Mar 31, 2006, 5:33:34 PM3/31/06
to miva-script
Those people at the USPS are useless for tech assistance. They would
not even explain why the server responds with certain error message.
They told me to google it and see if someone else had found out what
the errors mean....

Anyways, I am trying to use the rate calculator API for a non-merchant

app. The USPS API wants XML scipt to be sent to it. I've basically
generated the script they want using Miva. When I MvCall it, I get a
"Parameter is incorrect" message. If I paste the "action" link to the
IE address bar, it works ! ?. Makes no difference with get/post. It
seems to me that MvCall is adding or changing something. Has anyone
successfully interacted with this or any of the APIs of the USPS? I
removed my ID code in this sample.


Here is the most basic call made from my server:


<MvAssign name="cstring"
value="{'http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV2&XML=<RateV2Request

USERID=' $ asciichar(34) $ 'xxxx' $ asciichar(34) $ '><Package ID=' $
asciichar(34) $ '0' $ asciichar(34) $
'><Service>PRIORITY</Service><ZipOrigination>10022</ZipOrigination><ZipDest­ination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Contain­er>Flat

Rate Box</Container><Size>REGULAR</Size></Package></RateV2Request>'}">


<MvCall
action="&[cstring];"
method="post">
<MvEVAL EXPR="{callvalue}">
</MvCall>


The response from the the USPS server is the following html page source

that states there is a parameter error. But, If I paste the http link
below in the address bar, it works just fine and the USPS returns the
correct response.


<html>


http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV2&XML=<RateV2Request

USERID="xxxx"><Package
ID="0"><Service>PRIORITY</Service><ZipOrigination>10022</ZipOrigination><Zi­pDestination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Co­ntainer>Flat

Rate
Box</Container><Size>REGULAR</Size></Package></RateV2Request><br><br>


<html>
<head>
<title>
Error
</title>
</head>
<body>
The parameter is incorrect.
</body>
</html>
</html>


Here is the the reponse from the USPS server after pasting it directly
to the address bar:


<?xml version="1.0" ?>
- <RateV2Response>
- <Package ID="0">
<ZipOrigination>10022</ZipOrigination>
<ZipDestination>20008</ZipDestination>
<Pounds>10</Pounds>
<Ounces>5</Ounces>
<Container>Flat Rate Box</Container>
<Size>REGULAR</Size>
<Zone>3</Zone>
- <Postage>
<MailService>Priority Mail Flat Rate Box (11.25" x 8.75" x
6")</MailService>
<Rate>7.70</Rate>
</Postage>
- <Postage>
<MailService>Priority Mail Flat Rate Box (14" x 12" x
3.5")</MailService>
<Rate>7.70</Rate>
</Postage>
</Package>
</RateV2Response>


Am I overlooking something in the way I'm calling it from Miva?


Thanks

Bill Guindon

unread,
Mar 31, 2006, 5:52:06 PM3/31/06
to miva-...@googlegroups.com
On 3/31/06, J-vibe <housemu...@yahoo.com> wrote:
>
> Those people at the USPS are useless for tech assistance. They would
> not even explain why the server responds with certain error message.
> They told me to google it and see if someone else had found out what
> the errors mean....
>
> Anyways, I am trying to use the rate calculator API for a non-merchant
>
> app. The USPS API wants XML scipt to be sent to it. I've basically
> generated the script they want using Miva. When I MvCall it, I get a
> "Parameter is incorrect" message. If I paste the "action" link to the
> IE address bar, it works ! ?. Makes no difference with get/post. It
> seems to me that MvCall is adding or changing something. Has anyone
> successfully interacted with this or any of the APIs of the USPS? I
> removed my ID code in this sample.
>
>
> Here is the most basic call made from my server:
>
>
> <MvAssign name="cstring"
> value="{'http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV2&XML=<RateV2Request
>
> USERID=' $ asciichar(34) $ 'xxxx' $ asciichar(34) $ '><Package ID=' $
> asciichar(34) $ '0' $ asciichar(34) $
> '><Service>PRIORITY</Service><ZipOrigination>10022</ZipOrigination><ZipDest�ination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Contain�er>Flat
>
> Rate Box</Container><Size>REGULAR</Size></Package></RateV2Request>'}">


Can't tell how much of that is just a wrapping thing, or cut and
paste, but here's a couple quick thoughts:

You should encodeattribute() whatever you assign to XML. I also see
some '?' marks in the variable name, but I'll assume those aren't in
the actual code. Might pay to build XML up slowly, instead of one big
assign statement, easier to work with.


> <MvCall
> action="&[cstring];"
> method="post">
> <MvEVAL EXPR="{callvalue}">
> </MvCall>

Wow, a macro! Does this mean that Mr. Moon is coming back? ;)
You should upgrade that empressa, if nothing else, you should do
encodeentities() on cstring.


--
Bill Guindon (aka aGorilla)
The best answer to most questions is "it depends".

J-vibe

unread,
Mar 31, 2006, 5:58:40 PM3/31/06
to miva-script
Yeah I know about the macro thing but my app is several years old and I
have not had time to update it
the 100 miva pages or so. tried the encodeentities() and was told by
others using C and XML that the
USPS server rejects it. I tried it myself and the USPS server will not
accept it at all (even in the address bar).
Only the %20 spaces are accepted by the USPS server.

Bill Guindon

unread,
Mar 31, 2006, 6:34:15 PM3/31/06
to miva-...@googlegroups.com
On 3/31/06, J-vibe <housemu...@yahoo.com> wrote:
>

Odd. Well, you do have spaces in there, and you're not replacing them
with %20, maybe that's the problem?

viking...@gmail.com

unread,
Apr 3, 2006, 2:09:06 PM4/3/06
to miva-script
On another list, in some universe far far away, I once wrote:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

The most important consideration I came across was with the quote marks
in the xml string. Here's a portion from a shipping module that gets
the rates from USPS:


<MvASSIGN NAME = "XML" VALUE = "{'<IntlRateRequest
USERID=\"111VIKIN222\" PASSWORD=\"123456789\"><Package ID="0">...

Note that the quote marks for userid and password have been escaped but
other values aren't. You might want to try that.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=

J-vibe

unread,
Apr 13, 2006, 10:23:07 PM4/13/06
to miva-script
Thanks to all, I was able to solve this issue.

Bill Guindon

unread,
Apr 14, 2006, 12:23:09 AM4/14/06
to miva-...@googlegroups.com
What was the solution? Just curious (and I may run into it one day).

On 4/13/06, J-vibe <housemu...@yahoo.com> wrote:
>
> Thanks to all, I was able to solve this issue.
>
>
> >
>

J-vibe

unread,
Apr 17, 2006, 12:26:30 AM4/17/06
to miva-script
Here it is. This is one of two tests that the post office wants you to
try.
The array l.tokens will hold the XML packet sent back Then, you could
simply look through array for the fields you want. You need to place
your
own user ID assigned by the USPS in order to get this to work. There
are
many ways to do this, this one just happend to finally work for me.


<MvAssign name="l.order_export" value="{'API=RateV2&XML=<RateV2Request
USERID=' $ asciichar(34) $ 'XXXXXXXXXXXX' $ asciichar(34) $ '><Package


ID=' $ asciichar(34) $ '0' $ asciichar(34) $

'><Service>PRIORITY</Service><ZipOrigination>10022</ZipOrigination><ZipDestination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Container>Flat
Rate Box</Container><Size>REGULAR</Size></Package></RateV2Request>'}">
<MvAssign name="ncount" value="1">
<MvCALL ACTION = "http://testing.shippingapis.com/ShippingAPITest.dll"
METHOD = "XML"
FIELDS = "l.order_export">
<MvAssign name="l.tokens" index="{ncount}" value="{callvalue}">
<MvAssign name="ncount" value="{ncount + 1}">
<MvEval expr="{callvalue}">
</MvCall>

The array holds the following information which is sent back by the
USPS server, so using a simple loop,
you could find and assign the value to a variable.

<?xml version="1.0" ?>
<RateV2Response>


<Package ID="0">
<ZipOrigination>
10022
</ZipOrigination>
<ZipDestination>
20008
</ZipDestination>
<Pounds>
10
</Pounds>
<Ounces>
5
</Ounces>
<Container>
Flat Rate Box
</Container>
<Size>
REGULAR
</Size>
<Zone>
3
</Zone>

<Postage>
<MailService>
Priority Mail Flat Rate Box (11.25" x 8.75" x 6")
</MailService>
<Rate>
7.70
</Rate>
</Postage>

</Package>
</RateV2Response>

Reply all
Reply to author
Forward
0 new messages