---
title: Sending REST requests from code
related:
  - https://docs.kentico.com/13/integrating-3rd-party-systems/xperience-rest-service/configuring-the-rest-service.md
  - https://docs.kentico.com/13/integrating-3rd-party-systems/xperience-rest-service/authenticating-rest-requests.md
  - https://docs.kentico.com/13/integrating-3rd-party-systems/xperience-rest-service/getting-data-using-rest.md
  - https://docs.kentico.com/13/integrating-3rd-party-systems/xperience-rest-service/manipulating-data-using-rest.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

By sending requests to the [Xperience REST service](https://docs.kentico.com/13/integrating-3rd-party-systems/xperience-rest-service.md), you can [get data](https://docs.kentico.com/13/integrating-3rd-party-systems/xperience-rest-service/getting-data-using-rest.md) from the system or [manipulate pages and objects](https://docs.kentico.com/13/integrating-3rd-party-systems/xperience-rest-service/manipulating-data-using-rest.md).

The following example demonstrates how to send REST requests using server-side code. The sample code is incomplete – you need to perform the following according to your requirements:

- integrate the request sending into your custom logic or application
- supply the required values (HTTP method, request URL and data)
- process the response data
- ensure exception handling

```csharp title="C# example"

using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

...

string httpMethod; // "GET", "POST", "PUT" or "DELETE"
string requestUrl; // The URL of the REST request (base URL + resource path)
string requestData; // Data for POST or PUT requests

string responseDescription; // Stores the description of the response status
string responseData; // Stores data retrieved by the request

// Creates the REST request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);

// Sets the HTTP method of the request
request.Method = httpMethod;

// Authorizes the request using Basic authentication
request.Headers.Add("Authorization: Basic YWRtaW5pc3RyYXRvcjo=");

// Submits data for POST or PUT requests
if (request.Method == "POST" || request.Method == "PUT")
{
    request.ContentType = "text/xml";

    Byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(requestData);
    request.ContentLength = bytes.Length;

    using (Stream writeStream = request.GetRequestStream())
    {
        writeStream.Write(bytes, 0, bytes.Length);
    }
}

// Gets the REST response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Stores the description of the response status
responseDescription = (Int32)response.StatusCode + " - " + response.StatusDescription;

// Gets the response data
using (Stream responseStream = response.GetResponseStream())
{
    if (responseStream != null)
        using (StreamReader reader = new StreamReader(responseStream))
        {
            responseData = HttpUtility.HtmlEncode(reader.ReadToEnd());
        }
}

...

```
