CurlDotNet
CurlDotNet.Core
CurlResult Class
🎯 The response from your curl command - everything you need is here!
After running any curl command, you get this object back. It has the status code, response body, headers, and helpful methods to work with the data.
The API is designed to be intuitive - just type what you want to do:
- Want the body? →
result.Body - Want JSON? →
result.ParseJson<T>()orresult.AsJson<T>() - Want to save? →
result.SaveToFile("path") - Want headers? →
result.Headers["Content-Type"] - Check success? →
result.IsSuccessorresult.EnsureSuccess()
Quick Example:
var result = await Curl.Execute("curl https://api.github.com/users/octocat");
if (result.IsSuccess) // Was it 200-299?
{
var user = result.ParseJson<User>(); // Parse JSON to your type
result.SaveToFile("user.json"); // Save for later
}
public class CurlResult
Inheritance System.Object 🡒 CurlResult
Remarks
Design Philosophy: Every method name tells you exactly what it does. No surprises. If you guess a method name, it probably exists and does what you expect.
Fluent API: Most methods return 'this' so you can chain operations:
result
.EnsureSuccess() // Throw if not 200-299
.SaveToFile("backup.json") // Save a copy
.ParseJson<Data>() // Parse and return data
| Properties | |
|---|---|
| BinaryData | Binary data for files like images, PDFs, downloads. When you download non-text files, the bytes are here:`...` |
| Body | The response body as a string - this is your data! Contains whatever the server sent back: JSON, HTML, XML, plain text, etc. Common patterns:`...` Note: For binary data (images, PDFs), use BinaryData instead. Note: Can be null for 204 No Content or binary responses. |
| Command | The original curl command that was executed. Useful for debugging or retrying:`...` |
| Exception | Any exception if the request failed completely. Only set for network failures, not HTTP errors:`...` |
| Headers | All HTTP headers from the response - contains metadata about the response. Headers tell you things like content type, cache rules, rate limits, etc. Access them like a dictionary (case-insensitive keys). Get a specific header:`...` Check rate limits (common in APIs):`...` Common headers:... |
| IsBinary | Is this binary data? (images, PDFs, etc.) Quick check before accessing BinaryData:`...` |
| IsSuccess | Quick success check - true if status is 200-299. The easiest way to check if your request worked:`...` What's considered success: 200 OK, 201 Created, 204 No Content, etc. What's NOT success: 404 Not Found, 500 Server Error, etc. |
| OutputFiles | Files that were saved (if using -o flag). Track what files were created:`...` |
| StatusCode | The HTTP status code - tells you what happened. Common codes you'll see:`...` Example - Handle different statuses:`...` |
| Timings | Detailed timing information (like curl -w). See how long each phase took:`...` |
| Methods | |
|---|---|
| AppendToFile(string) | Append response to an existing file. Add to a file without overwriting:`...` |
| AsJson<T>() | Parse JSON response (alternative name for AsJson, some prefer ParseJson. Both methods are identical and produce the same result.`...` |
| AsJsonDynamic() | Parse JSON as dynamic object (when you don't have a class). Useful for quick exploration or simple JSON structures. This method returns a dynamic object that allows you to access JSON properties without defining a C# class. However, there's no compile-time checking, so prefer ParseJson<T>() with typed classes when possible. Example:`...` |
| EnsureContains(string) | Throw if response body doesn't contain expected text. Validate response content:`...` |
| EnsureStatus(int) | Throw if status doesn't match what you expect. Validate specific status codes:`...` |
| EnsureSuccess() | Throw an exception if the request wasn't successful (not 200-299). Use this when you expect success and want to fail fast. This matches curl's -f (fail) flag behavior. |
| FilterLines(Func<string,bool>) | Extract lines that match a condition. Filter text responses:`...` |
| GetHeader(string) | Get a specific header value (case-insensitive). Easy header access with null safety. This matches curl's header behavior exactly. |
| HasHeader(string) | Check if a header exists. Test for header presence before accessing. This is case-insensitive, matching curl's behavior. |
| ParseJson<T>() | Parse the JSON response into your C# class. The most common operation - turning JSON into objects. This method uses System.Text.Json.JsonSerializer in .NET 6+ and Newtonsoft.Json.JsonConvert in .NET Standard 2.0 for maximum compatibility. Example:`...` Tip: Use https://json2csharp.com to generate C# classes from JSON! |
| Print() | Print status code and body to console. More detailed debug output:`...` |
| PrintBody() | Print the response body to console. Quick debugging output:`...` |
| PrintVerbose() | Print everything - status, headers, and body (like curl -v). Full debug output:`...` |
| Retry() | Retry the same curl command again. Simple retry for transient failures:`...` |
| RetryWith(Action<CurlSettings>) | Retry with modifications to the original command. Retry with different settings:`...` |
| SaveAsCsv(string) | Save JSON response as CSV file (for JSON arrays). Converts JSON arrays to CSV for Excel:`...` Note: Only works with JSON arrays of objects. |
| SaveAsJson(string, bool) | Save as formatted JSON file (pretty-printed). Makes JSON human-readable with indentation:`...` |
| SaveToFile(string) | Save the response to a file - works for both text and binary! Smart saving - automatically handles text vs binary:`...` Path examples:`...` |
| SaveToFileAsync(string) | Save the response to a file asynchronously. Same as SaveToFile but doesn't block:`...` |
| ToStream() | Convert the response to a Stream for reading. Useful for streaming or processing data:`...` |
| Transform<T>(Func<CurlResult,T>) | Transform the result using your own function. Extract or convert data however you need:`...` |