From 72b15678a1f66a8683034e30fefa51969a7ac4d5 Mon Sep 17 00:00:00 2001 From: Alpha Shuro Date: Wed, 12 Oct 2016 13:58:34 +0200 Subject: [PATCH] Add example for posting json content i struggled to figure out how to post json content with nim's http client. this is a fundamental capability in many web apps, we don't always need to send data as multipart form data (e.g. when communicating via json apis) so frankly i'm surprised it isn't part of the "post" and "postContent" procs --- lib/pure/httpclient.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 4404a94267..bfeb9a52ed 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -50,6 +50,20 @@ ## ## echo client.postContent("http://validator.w3.org/check", multipart=data) ## +## You can also make post requests with custom headers. +## This example sets ``Content-Type`` to ``application/json`` +## and uses a json object for the body +## +## .. code-block:: Nim +## import httpclient, json +## +## let client = newHttpClient() +## client.headers = newHttpHeaders({ "Content-Type": "application/json" }) +## let body = %*{ +## "data": "some text" +## } +## echo client.request("http://some.api", httpMethod = HttpPost, body = $body) +## ## Progress reporting ## ================== ##