VB and REST API

Hello all,

I need to use this API:

http://help.cj.com/en/web_services/web_services.htm#Commission_Detail_Service.htm

to bring CJ comissions.... anyone know how to make this possible? I never worked with APIs.

Thanks!
pvg1975Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Since it's a REST API, you can more or less think of it like calling a web page. You could type the address in a web browser and (possibly) see the result of the call. There are examples of what the URLs should look like on the page you linked to. You just need to use the appropriate class to make web requests to their site.

It seems as though the provider may be serving requests via the GET method (what most web pages use). You'll need to check with the provider or their documentation to confirm, though.

You should be able to use either the HttpRequest class or the WebClient class to make requests to the provider's URLs.
pvg1975Author Commented:
Hi Kaufmed!

I tried this:

https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2011-02-15&end-date=2011-02-17

And I get the message "You must specify a developer key"

I have a developer key, but I dont know where to place it

Thanks!
Bob LearnedCommented:
If you look at the documentation for the request:

http://help.cj.com/en/web_services/web_services.htm#Commission_Detail_Service.htm

the value is passed in the request headers:


Commissions Resource Request Parameters

Header = authorization
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

pvg1975Author Commented:
Yes, I noticed that, but I dont know how to pass the header, and I get an error when trying to add https://commission-detail.api.cj.com/v3/commissions as a service reference in VB.NET
Bob LearnedCommented:
REST services are not web services that you would be able to add a service reference to.  Adding a header value depends on what you are using to send the web request.

Example:

WebHeaderCollection.Add Method (String, String)
http://msdn.microsoft.com/en-us/library/y280s7kx.aspx

 'Create a web request for "www.msn.com".
            Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)

            'Get the headers associated with the request.
            Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers

   	    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method")

	    'Add the Accept-Language header (for Danish) in the request.
            myWebHeaderCollection.Add("Accept-Language:da")

            'Include English in the Accept-Langauge header. 
            myWebHeaderCollection.Add("Accept-Language","en;q" + ChrW(61) + "0.8")

Open in new window

pvg1975Author Commented:
Thanks!

I tried this code:

        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://commission-detail.api.cj.com/v3/commissions"), HttpWebRequest)
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        myWebHeaderCollection.Add("authorization:**** Private information removed ***")
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        MsgBox(myWebHeaderCollection)
        myHttpWebResponse.Close()

But i get this error:

The remote server returned an error: (400) Bad Request.
Bob LearnedCommented:
Here is a problem with a REST service--if there is an error on the server it always returns 400 - Bad Request.

I would try this instead:

myWebHeaderCollection.Add("authorization", <authorization key>)
Bob LearnedCommented:
*** Private information removed ***

.NET Zone Advisor
Bob LearnedCommented:
Also, you need to conform to the URL for each type of request.  All required elements must be specified.  If you look at the help for the web service, it explains what is optional and what is required.

Request examples:

https://commission-detail.api.cj.com/v3/commissions?date-type=event

https://commission-detail.api.cj.com/v3/commissions?date-type=posting
Bob LearnedCommented:
Another thing to think about is getting the HttpWebResponse, getting the response stream, and extracting the text from the stream using a StreamReader.  The server might return information that can help you debug a request.
pvg1975Author Commented:
Thanks TLO,

I tried this and still get the same error:

        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2012-02-15&end-date=2012-03-21"), HttpWebRequest)
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        myWebHeaderCollection.Add("authorization", "<My_Key>")
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        MsgBox(myWebHeaderCollection)
        myHttpWebResponse.Close()

Im kinda feeling like an idiot lol... I just cant make it work :(

VB is returning the error on this line:

Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

Thanks for your help again.
Bob LearnedCommented:
Here is a perfect opportunity to learn something, because this is a common problem with REST web services.  

1) This is an HTTPS address, which can be little tricky with HttpWebRequest.

2) You need to match up the expected request method (GET, PUT, POST, DELETE).  

3) You can use Fiddler (an HTTP Debugger) to generate a known web request.

Fiddling with Fiddler – debugging REST services on your machine
http://herbjorn.wordpress.com/2010/04/27/fiddling-with-fiddler-debugging-rest-services-on-your-machine/

4) Are you examining the web response text?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pvg1975Author Commented:
Hi TLO!

Looks like I made it work, I get an XML as a response!

I will create a new question regarding this in a few minutes.

Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.