Solved

VB and REST API

Posted on 2012-03-23
13
785 Views
Last Modified: 2012-03-23
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!
0
Comment
Question by:pvg1975
  • 7
  • 5
13 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37757707
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.
0
 

Author Comment

by:pvg1975
ID: 37757855
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!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758150
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
0
 

Author Comment

by:pvg1975
ID: 37758172
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758191
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

0
 

Author Comment

by:pvg1975
ID: 37758256
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758600
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>)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758604
*** Private information removed ***

.NET Zone Advisor
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758625
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758630
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.
0
 

Author Comment

by:pvg1975
ID: 37758757
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 37759038
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?
0
 

Author Comment

by:pvg1975
ID: 37759705
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!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now