REST API for USPS CityStateLookup API

Hi everyone, I am brand new to WeWeb and the community, this is my first post. Thanks to the WeWeb team for creating this amazing tool and thanks to the community for having me.

I do not have any coding experience. I am trying to implement the United States Postal Service (USPS) CityStateLookup API. This API takes a 5-digit Zip code as input (along with a UserID) and returns the associated city and state. I am trying to implement this dynamically on a number input field, taking the number input by the user and passing that to the API. This is my first attempt at implemting a REST API in WeWeb.

The API documentation is here.

This is what I have so far in the WeWeb workflow editor. As you can see my request is making it through but I seem to be receiving an error response.

The full URL that I have in there is: https://secure.shippingapis.com/ShippingAPI.dll?API=CityStateLookup

I was able to get a successful response by entering the below into my browser, but I can’t figure out how to correctly set this up in WeWeb.

Any suggestions please? Thanks!

http://secure.shippingapis.com/ShippingAPI.dll?API=CityStateLookup
&XML=<CityStateLookupRequest USERID="xxxxxxx"><ZipCode ID= "0">
<Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>

Hey @Dorian :wave:

Glad you like WeWeb!

From what I understand from the documentation, you should send the Zipcode and UserID inside the request as fields in an XML format.

To do this, you should first use a POST request type, as GET doesn’t accept fields (fields are sent differently to the API than querystrings, that are sent in the URL).

As WeWeb doesn’t handle XML natively, you should use a custom binding and use JavaScript string concatenation to replace Zip5 and UserID by your variables.

Also, I’ve noticed the API accept only requests from servers, not clients (like browsers).

In the end, it should look something like this:

Hope this helps!

Thanks so much for the thorough reply @Quentin! Unfortunately it doesn’t seem to be working. Please see attached screenshot. I think I have it all setup per your screenshot, but it seems the actual variable value is not being included in the generated XML - is that right?

Is there some way for me to check the generated URL so I can see what is going wrong?

My bad, you need to add a dollar sign $ before each curly brace, like so: ${variable['XXX']}

Amazing that worked to populate the variable values, thanks @Quentin! But… I’m still getting a syntax error response. Any other ideas please? Is there some way for me to see the URL generated by WeWeb so I can check what’s going wrong there?

I took a different approach and got it to work. I simply built the URL using the Concatenate formula. But I’m wondering is this acceptable? Am I breaking any best practices using this approach?

I tell people not to worry about best practices early on - especially when working with something quite complicated like XML. There is a more direct document-building approach you can apply with JS (related to my reply on a different thread) but if you can get what you need using this kind of string-building approach, I’d celebrate the win and move on to the next piece of functionality.

I’m not saying this is for the ages: you might come back to this later, but you will be smarter and more experienced by then, making a future reconsideration much sharper and cheaper than one done today.

2 Likes

OK I’m good with that! Thanks @raydeck.

Actually, as @raydeck mentioned, I wouldn’t worry at first.

Also, my bad again, as the API was using the URL when I thought it should be sent in the request’s payload. So I was wrong again :confused:

1 Like

No worries @Quentin, thank you for your help!