RestSharp
API
request body
text manipulation
C#

How to add text to request body in RestSharp

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Adding text to a RestSharp request body is mostly about matching the API contract. If the server expects raw text, send raw text with the correct content type. If it expects JSON, use the JSON helpers or send a JSON string intentionally rather than treating every payload as generic text.

The most common bugs here are not about RestSharp syntax. They come from sending the right body with the wrong content type, or mixing several body-building methods on one request.

Once you align payload shape and content type, the RestSharp part of the problem is usually very small.

Send Plain Text Explicitly

If the endpoint expects a raw text/plain body, use AddStringBody:

csharp
1using RestSharp;
2using System.Threading.Tasks;
3
4public static async Task SendPlainTextAsync()
5{
6    var client = new RestClient("https://api.example.com");
7    var request = new RestRequest("notes", Method.Post);
8
9    request.AddStringBody("hello from RestSharp", DataFormat.None);
10    request.AddHeader("Content-Type", "text/plain");
11
12    var response = await client.ExecuteAsync(request);
13    System.Console.WriteLine(response.StatusCode);
14}

This makes it clear that you are sending a raw string, not a serialized object.

Send JSON as JSON

Sometimes people say "text body" when the real payload is JSON represented as text. If you have a .NET object, AddJsonBody is usually the cleaner option:

csharp
1using RestSharp;
2using System.Threading.Tasks;
3
4public record MessageRequest(string Text, string Author);
5
6public static async Task SendJsonAsync()
7{
8    var client = new RestClient("https://api.example.com");
9    var request = new RestRequest("messages", Method.Post);
10
11    request.AddJsonBody(new MessageRequest("hello", "mark"));
12
13    var response = await client.ExecuteAsync(request);
14    System.Console.WriteLine(response.Content);
15}

This lets RestSharp handle serialization and the JSON content type for you.

Send a Raw JSON String Only When You Mean To

If the JSON already exists as a string and should be forwarded unchanged, send it intentionally:

csharp
1using RestSharp;
2using System.Threading.Tasks;
3
4public static async Task ForwardJsonAsync(string rawJson)
5{
6    var client = new RestClient("https://api.example.com");
7    var request = new RestRequest("ingest", Method.Post);
8
9    request.AddStringBody(rawJson, ContentType.Json);
10
11    var response = await client.ExecuteAsync(request);
12    System.Console.WriteLine(response.IsSuccessful);
13}

In this case you are taking responsibility for the validity of the JSON string yourself.

Prefer Async Requests

HTTP calls are I/O-bound, so ExecuteAsync should be the default in modern .NET code. That keeps threads free while the request is in flight and fits naturally with ASP.NET or background worker code.

The larger principle is simple:

  • use plain text helpers for plain text
  • use JSON helpers for JSON
  • keep the content type aligned with the payload

Once those three things agree, RestSharp is usually straightforward.

Verify What the Server Actually Receives

If a request is failing, inspect the outgoing request with server logs or an HTTP proxy. Many "RestSharp body bugs" are really contract mismatches:

  • wrong content type
  • wrong body shape
  • body omitted because a different helper overrode it

Looking at the actual bytes on the wire is often faster than staring at client code.

That verification step is especially useful when you are integrating with a third-party API that documents the body format loosely.

Common Pitfalls

  • Sending plain text while leaving the content type as application/json.
  • Building JSON manually when AddJsonBody would serialize it safely.
  • Mixing several body-building methods on the same request.
  • Using synchronous execution where ExecuteAsync would fit better.
  • Debugging only the client side without checking what the server received.

Summary

  • Use AddStringBody for genuine raw text payloads.
  • Use AddJsonBody for structured JSON objects.
  • If you must send a prebuilt string, pair it with the correct content type.
  • Prefer ExecuteAsync for HTTP requests in modern C# code.
  • Match the request body shape and content type to the server contract.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.