3

I'm new round here so please be kind.

I need to make a simple request to Sears to retrieve details about a product . I'm not very used to json/xml and with server requests.

As I've already registered to Sears, I have the API key.

My code goes like this:

    protected void Page_Load(object sender, EventArgs e)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.developer.sears.com/v1/productdetails?&store=Sears&contentType=xml");

        request.KeepAlive = false;
        request.Method = "GET";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream());
        string respond = sr.ToString();
        Label1.Text = respond;
    }

I get the 401 - Unauthorized error

The questions are:

  1. Where and how do I use the API key ?
  2. As I suppose i will get an unformatted xml, how do I display it properly ?

Thanks !

VladU
  • 75
  • 1
  • 7
  • The APIs must document how to use the API key. If you are displaying in internet explorer no formatting is required. Even otherwise it may not be. If you have to, you can use `tidy` or some such library to format it. – Miserable Variable Mar 27 '12 at 21:52

1 Answers1

3

According their API documentation, you need to include the API Key as one of the parameters in your query request.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.developer.sears.com/v1/productdetails?apikey=<apikey>&store=Sears&partNumber=05408391000P");

http://developer.sears.com/api/Product%2520Details%2520API/Hard%2520line%2520products/doc

Then change your last two lines of code to:

string respond = sr.ReadToEnd();
Label1.Text = respond; 
mgnoonan
  • 7,060
  • 5
  • 24
  • 27
  • Thanks for the fast answer. I did that and I can get the request from server. Now, my label displays "System.IO.StreamReader" and not the product info. How do I get xml/json from request ? shall I change the request ? – VladU Mar 27 '12 at 22:02
  • Now my label gets a text like "05408391000P 40821SDBB $69.95 each ]]> 44411656 10153 12605 -1 1 1 0 0 41442744 false 0 0 69.95 69.95 false false 05408391000P false false false 05408391000P 2 false false true" I think is Xml ?? but is not displayed or parsed properly. Can you help me plese showing it properly ? What if i change contentType=xml to contentType=json how do I parse that to show it properly ? – VladU Mar 28 '12 at 12:43
  • Are you familiar with deserializing XML or JSON into objects? – mgnoonan Mar 28 '12 at 12:52
  • I've read a few tutorials, but I don't know where to begin from in my code – VladU Mar 28 '12 at 12:55
  • Here's an example, although this may be more advanced than you are ready for: http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – mgnoonan Mar 28 '12 at 13:02