# HttpClient

Kind: Class · Namespace: Sansar.Simulation · Assembly: Sansar.Simulation.dll · Script API 47.10.0

<div class="api-meta">
<span class="api-kind api-kind-class" data-pagefind-filter="kind:Class">Class</span>
<span data-pagefind-filter="namespace:Sansar.Simulation">Namespace <a href="/scripting/api/sansar.simulation"><code>Sansar.Simulation</code></a></span>
<span>Inherits <a href="/scripting/api/sansar.script/instanceinterface">Sansar.Script.InstanceInterface</a></span>
</div>

<span id="T:Sansar.Simulation.HttpClient"></span>

The Http Client can be used to make HTTP requests to external services.

```csharp
public class HttpClient : Sansar.Script.InstanceInterface
```

## Remarks

A simple script to get some external data might be:

## Example

```csharp title="HttpClientExample.cs"
/* This content is licensed under the terms of the Creative Commons Attribution 4.0 International License.
 * When using this content, you must:
 * •    Acknowledge that the content is from the Sansar Knowledge Base.
 * •    Include our copyright notice: "2022 Sansar, Inc."
 * •    Indicate that the content is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.
 * •    Include the URL for, or link to, the license summary at https://creativecommons.org/licenses/by-sa/4.0/deed.hi (and, if possible, to the complete license terms at https://creativecommons.org/licenses/by-sa/4.0/legalcode.
 * For example:
 * "This work uses content from the Sansar Knowledge Base. © 2022 Sansar, Inc." Licensed under the Creative Commons Attribution 4.0 International License (license summary available at https://creativecommons.org/licenses/by/4.0/ and complete license terms available at https://creativecommons.org/licenses/by/4.0/legalcode)."
 */
using Sansar.Script;
using Sansar.Simulation;
using System;
using System.Collections.Generic;
using System.Text;


public class HttpClientExample : SceneObjectScript
{
    public override void Init()
    {
        HttpRequestOptions options = new HttpRequestOptions();

        options.Parameters = new Dictionary<string, string>()
        {
            { "num" , "1" },
            { "min" , "100000000" },
            { "max" , "999999999" },
            { "base" , "10" },
            { "format" , "plain" },
            { "col" , "1" },
        };

        ScenePrivate.User.Subscribe(User.AddUser, userData=>NewUser(userData, options));
    }

    void NewUser(UserData userData, HttpRequestOptions options)
    {
        AgentPrivate agent = ScenePrivate.FindAgent(userData.User);

        if (agent != null)
        {
            var result = WaitFor(ScenePrivate.HttpClient.Request, "https://www.random.org/integers/", options) as HttpClient.RequestData;
            if (result.Success)
            {
                agent.SendChat("Your lucky number is " + result.Response.Body);
            }
        }
    }
}
```

<p class="api-example-source">Source: <code>Examples/HttpClientExample.cs</code> in the Sansar 47.10.0 client ScriptApi snapshot.</p>

## Members at a glance

| Kind | Members |
|---|---|
| **Methods** | [Request](#request) |

## Methods

### Request

<div class="api-card" id="request-string-action">
<span id="M:Sansar.Simulation.HttpClient.Request(System.String,System.Action{Sansar.Simulation.HttpClient.RequestData})"></span>

```csharp signature
public void Request(
    string url,
    Action<HttpClient.RequestData> handler
)
```

Performs a HTTP request.

| Parameter | Type | Description |
|---|---|---|
| `url` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The url. It may include parameters, but they must be url-encoded. |
| `handler` | [Action&lt;HttpClient.RequestData&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The event handler to be called when the request is complete |

<details class="api-remarks"><summary>Remarks</summary>

Currently only text content types are supported.

</details>

</div>

<div class="api-card" id="request-string-httprequestoptions-action">
<span id="M:Sansar.Simulation.HttpClient.Request(System.String,Sansar.Simulation.HttpRequestOptions,System.Action{Sansar.Simulation.HttpClient.RequestData})"></span>

```csharp signature
public void Request(
    string url,
    HttpRequestOptions requestOptions,
    Action<HttpClient.RequestData> handler
)
```

Performs a HTTP request.

| Parameter | Type | Description |
|---|---|---|
| `url` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The url. It may include parameters, but they must be url-encoded. |
| `requestOptions` | [HttpRequestOptions](/scripting/api/sansar.simulation/httprequestoptions) | The request options struct |
| `handler` | [Action&lt;HttpClient.RequestData&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The event handler to be called when the request is complete |

<details class="api-remarks"><summary>Remarks</summary>

Currently only text content types are supported.

</details>

</div>


## Used in examples

- [HttpClientExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/HttpClientExample.cs)

<p class="api-generated" data-pagefind-ignore>Generated from the Sansar 47.10.0 client ScriptApi snapshot.</p>