Jan 26

I was recently asked how to POST a Raw String Body Using DalSoft RestClient, this reminded me that the documentation for DalSoft RestClient is a little out of date.

Updating the docs has been on my todo list for a while now, so I’ve made a new years resolution to get this done. I’m going to start with a series of posts about what you can do with DalSoft RestClient, I’ll then rework these posts into the new docs.

Here’s the first post it’s a quick one – HTTP POST a Raw String Body Using C#

var client = new RestClient("https://your-server/api");
var response = await client.Resource("your-api-resource").Post("string that you want to post");

Yes you can do this as one liner with HttpClient or using one of the plethora of nuget Rest Client Packages, but you would be missing out on easy testing and pipelines then!

Like what you see? There’s a lot more head over to the DalSoft RestClient docs and GitHub Repo.

Tagged with:
Nov 12

Want to learn how test your REST API fluently in a couple of lines of C# code?

Like this:

public async Task Get_UserWithId1_ReturnsDynamicWithUsernameBretAndOkStatusCode()
   var client = new RestClient("https://jsonplaceholder.typicode.com", new Config()
                .SetJsonSerializerSettings(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }));

   await client
      .Verify(userIsBret => userIsBret.username == "Bret")
      .Verify(httpResponseMessageIsOk => httpResponseMessageIsOk.HttpResponseMessage.StatusCode == HttpStatusCode.OK);

View live example

Head over to my post on Code Maze, and your be testing REST API’s using C# in no time at all.

Tagged with:
Aug 04

Since starting the project in Jan 2015 one of the most asked for features has been full static support (you have always been able to cast responses via duck typing). Inspired by libraries written in dynamic languages like JavaScript and Python – the idea behind RestClient was to reduce the fiction and ceremony when working with Rest API’s, and later on HTTP in general.

I’ve always wanted to add static typing to be more inline with what is expected from a C# library, but wanted to do it in such way that the original concept remained, and that all code is backwards compatible. I’ve now spent the time required to change the package, and this is the result:

DalSoft Rest Client https://github.com/DalSoft/DalSoft.RestClient

Continue reading »

Tagged with:
Feb 22

CodeMaze have written a great post with real world examples of using C# and DalSoft.RestClient to consume any REST API. It includes everything you need to know head over there now if you find yourself writing a lots of boilerplate code to consume REST API’s.

Tagged with:
Nov 16

Before 3.3.1 if you wanted to control serialization you would have to decorate your model with attributes, for example if your REST API used snake case naming convention:

public class User
  public string PhoneNumber { get; set; }

As of 3.3.1 you can provide JsonSerializerSettings to the config – this saves you having to add the attributes to properties individually, which will make your code cleaner. For example if your REST API used snake case naming convention:

// When directly creating a RestClient instance
dynamic restClient = new RestClient("https://dalsoft.co.uk", new Config()
	.SetJsonSerializerSettings(new JsonSerializerSettings { ContractResolver =  new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } }));

// When using IHttpClientFactory
	.AddRestClient(Name, "https://dalsoft.co.uk")
	.SetJsonSerializerSettings(new JsonSerializerSettings { ContractResolver =  new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } });

Now the SnakeCaseNamingStrategy ContractResolver is used by default when deserializing responses from your REST API.

To find out more about DalSoft.RestClient including the new JsonSerializerSettings head over to https://restclient.dalsoft.io

Tagged with:
preload preload preload