Solved

VB and REST API

Posted on 2012-03-23
13
804 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 75

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

635 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