Generate .NET HTTP clients from cURL

Hamza Sabljakovic
3 min readNov 17, 2019

--

Recently, we’ve been working with quite a few integrations. All of them were via public APIs (REST, graphql, etc.). Some of them had SDKs for c# (hiding the HTTP client boilerplate code), but unfortunately, most of them didn’t. As we saw a noticeable difference in productivity when comparing integrations with and without SDK, we started looking into ways to eliminate the boilerplate.

Upon closer inspection, we noticed that the one thing that all of those API docs have are cURL examples. It appears that cURL is the de facto standard for the API documentation. Having said that, we thought, there must be a tool that can do the conversion from one format to another. And, we were right, there is a project solving our problem. Although, a great tool, there were still some parts that required manual text (code) formatting, extraction, etc. The steps that you don’t want to do by hand, so, we decided to look for more tools that can help us automate all the steps.

In the rest of this post, we will explore how we combined a couple of free websites to generate a .NET HTTP client from curl in a couple of seconds.

For this exercise, we will use the Snapchat advertisement API. The cURL example we are going to utilize is the following:

curl -X POST \
-H "Authorization: Bearer meowmeowmeow" \
-H "Content-Type: application/json" \
-d '{"campaigns": [{"name": "Cool Campaign", "ad_account_id": "3b0fbace-04b4-4f04-a425-33b5e0af1d0d", "status": "PAUSED", "start_time": "2016-08-11T22:03:58.869Z"}]}' \
"https://adsapi.snapchat.com/v1/adaccounts/{ad_acount_id}/campaigns"

Step 1. — cURL to httpClient request

The first step is pretty straightforward, in the already mentioned tool, https://curl.olsh.me, paste you curl.

cURL to c# converter

The majority of the autogenerated code is fine, the part which we are not so happy with, is the

"{\"campaigns\": [{\"name\": \"Cool Campaign\", \"ad_account_id\": \"3b0fbace-04b4-4f04-a425-33b5e0af1d0d\", \"status\": \"PAUSED\", \"start_time\": \"2016-08-11T22:03:58.869Z\"}]}"

For it to be useable, we need a way to convert it into a c# class. Transforming JSON into a c# class it a fairly popular approach (see step 3.), however, the output from the above tool is not a valid JSON. The problem with the output is that quotes () are prefixed with an escape character (\).

Step 2. — “Unescape” string

Luckily, there is another website that can do the conversion for us.
https://www.freeformatter.com/java-dotnet-escape.html

Remove the escape character

Now that we have a valid JSON we can move to the third and final step of this exercise.

Step 3. — Convert JSON into c#

The third one is pretty standard, most of you have used it before. We simply take the output from the previous step and “pipe” it into the quick type.

Conclusion

A quick and easy way to autogenerate boilerplate code and focus on building things that matter to your business. Of course, there are still parts that could be improved, for example, the response object should also be converted into a c# class to make it more developer-friendly. Anyway, the process is not perfect, it involves a few manual steps, but still, it saved us time and we hope it can to do the same for you.

--

--

Hamza Sabljakovic
Hamza Sabljakovic

Written by Hamza Sabljakovic

Software engineer based in Stockholm, Sweden.

No responses yet