2

I want to send the sitecore data to authenticated user only..How to pass the HttpWebRequest object to DownloadString function of webclient....DownloadString cannot take HttpWebRequest as a parameter.I was following this link Sitecore 7.2 - Item Web API-User Authentication

var client = new WebClient();

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://gyldendal.local/-/item/v1/?sc_itemid={110D8759F-DEA9-42EA-9C1C-8A5DF7E70EF9}&sc_database=master");

    request.Headers["X-Scitemwebapi-Username"] = "admin";
    request.Headers["X-Scitemwebapi-Password"] = "b";
 var apiResponse = client.DownloadString(request);
            dynamic jsonResponse = JObject.Parse(apiResponse);

3 Answers 3

3

...and here is a way to do it using the Web API framework; just because i am a fan of async.

using (var client = new HttpClient())
            {
                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri("..."),
                    Method = HttpMethod.Get
                };

                request.Headers.Add("X-Scitemwebapi-Username", "sitecore\admin");
                request.Headers.Add("X-Scitemwebapi-Password", "b");

                var response = await client.SendAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    var responseString= await response.Content.ReadAsStringAsync();
                    ...
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

2

You can get the response from the request object directly.

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("...");

    request.Headers["X-Scitemwebapi-Username"] = "sitecore\admin";
    request.Headers["X-Scitemwebapi-Password"] = "b";

    //(...)

    using (var response = (HttpWebResponse)request.GetResponse())
    {
       var stream = response.GetResponseStream();
       var reader = new StreamReader(stream);

       string apiResponse = reader.ReadToEnd();
    }

Also, you need to inform the domain in the user name. Like sitecore\admin

Cheers

3 Comments

Hello robert, what is response here?
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); but still this gives me {"statusCode":401,"error":{"message":"Access to site is not granted."}}
Sorry Salman. I edited the answer to include the response object
1

I happen to like using RestSharp over HttpWebRequest, so thought I'd add another version to the list:

// Build a client pointing to your sitecore instance
IRestClient restClient = new RestClient("http://gyldendal.local/");
// Add credentials to all future requests
restClient.AddDefaultHeader("X-Scitemwebapi-Username", @"sitecore\admin");
restClient.AddDefaultHeader("X-Scitemwebapi-Password", "b");

// Build a request to the item api
IRestRequest restRequest = new RestRequest("/-/item/v1/", Method.GET);
// specify API parameters
restRequest.AddParameter("sc_database", "master");
restRequest.AddParameter("sc_itemid", "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}");

#if DEBUG
Console.WriteLine( restClient.BuildUri(restRequest) ); // http://gyldendal.local/-/item/v1/?sc_database=master&sc_itemid={110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}
#endif

// Execute request
IRestResponse restResponse = restClient.Execute(restRequest);

// Confirm successful response
if (restRequest.StatusCode == HttpStatusCode.OK)
{
    /* Work with restResponse.Content */
#if DEBUG
    Console.WriteLine(restResponse.Content); // {"statusCode":200,"result":{....}}
#endif
}

Also, just so it's clear, confirm the itemwebapi has been enabled on your specific <site>. For a default install, you can use the following patch file to enable it:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sites>
      <site name="website">
        <!-- Options: Off|StandardSecurity|AdvancedSecurity -->
        <patch:attribute name="itemwebapi.mode">StandardSecurity</patch:attribute>
        <!-- Options: ReadOnly|ReadWrite -->
        <patch:attribute name="itemwebapi.access">ReadOnly</patch:attribute>
        <!-- Options: True|False -->
        <patch:attribute name="itemwebapi.allowanonymousaccess">False</patch:attribute>
      </site>
    </sites>
  </sitecore>
</configuration>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.