Explicit Request Contexts

2 min read

2 min read

2 min read

2 min read

Request contexts allow a client to send additional data to the server without having to include this data as input parameters in the operation’s Slice definition. They provide a lightweight mechanism for passing metadata alongside normal request arguments.

The Ice context demo provides a complete example of using request context in C++.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

C++
// We request a French greeting by setting the context parameter.
string greeting = greeter->greet(name, {{"language", "fr"}});

On the server side, the request context is available through the ctx member of the Ice::Current parameter:

C++
string
Server::Chatbot::greet(string name, const Ice::Current& current)
{
// We retrieve the value for the language entry in the context.
auto p = current.ctx.find("language");
string language = p != current.ctx.end() ? p->second : "";
...
}

The Ice context demo provides a complete example of using request context in C#.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

C#
// We request a French greeting by setting the context parameter.
string greeting = await greeter.GreetAsync(
name,
context: new Dictionary<string, string> { ["language"] = "fr" });

On the server side, the request context is available through the ctx member of the Ice.Current parameter:

C#
public override string Greet(string name, Ice.Current current)
{
// We retrieve the value for the language entry in the context.
if (!current.ctx.TryGetValue("language", out string? language))
{
language = "";
}
...
}

The Ice context demo provides a complete example of using request context in Java.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

Java
// We request a French greeting by setting the context parameter.
String greeting = greeter.greet(name, Map.of("language", "fr"));

On the server side, the request context is available through the ctx member of the com.zeroc.Ice.Current parameter:

Java
public String greet(String name, Current current) {
// We retrieve the value for the language entry in the context.
String language = current.ctx.getOrDefault("language", "");
...
}

The Ice context demo provides a complete example of using request context in JavaScript.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

JavaScript
// We request a French greeting by setting the context parameter.
let greeting = await greeter.greet(name, new Map([["language", "fr"]]));

On the server side, the request context is available through the ctx member of the Ice.Current parameter:

TypeScript
greet(name: string, current: Ice.Current): string | Promise<string> {
// We retrieve the value for the language entry in the context.
const language: string = current.ctx.get("language") || "";
...
}

The Ice context demo provides a complete example of using request context in MATLAB.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

MATLAB
% Create a request context.
context = configureDictionary('string', 'string');
% We request a French greeting by setting 'language' in the context parameter.
context('language') = 'fr';
greeting = greeter.greet(name, context);

The Ice context demo provides a complete example of using request context in PHP.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

PHP
// We request a French greeting by setting 'language' in the context parameter.
$context = ["language" => "fr"];
$greeting = $greeter->greet(get_current_user(), $context);

The Ice context demo provides a complete example of using request context in Python.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

Python
# We request a French greeting by setting the context parameter.
greeting = await greeter.greetAsync(name, context={"language": "fr"})

On the server side, the request context is available through the ctx member of the Ice.Current parameter:

Python
def greet(self, name: str, current: Ice.Current) -> str:
# We retrieve the value for the language entry in the context.
language = current.ctx.get("language", "")

The Ice context demo provides a complete example of using request context in Ruby.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

Ruby
# We request a French greeting by setting the context parameter.
greeting = greeter.greet(Etc.getlogin, {"language"=>"fr"})

The Ice context demo provides a complete example of using request context in Swift.

Using the Slice greeter definitions once again:

Slice
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}

A client application can set a request context to send additional metadata:

Swift
// We request a French greeting by setting the context parameter.
var greeting = try await greeter.greet(name, context: ["language": "fr"])

On the server side, the request context is available through the ctx member of the Ice.Current parameter:

Swift
func greet(name: String, current: Ice.Current) -> String {
// We retrieve the value for the language entry in the context.
let language = current.ctx["language"] ?? ""
...
}