Search code examples
c#restsharpnotion-api

Notion API Update page using restSharp


I try to use RestSharp to update a page on Notion buy I have an error "Error parsing JSON body"

private RestRequest WebRequestWithParam(string url, Method method, string param)
    {
        var request = new RestRequest(url, method);
        request.AddHeader("Authorization", $"Bearer {apiKey}");
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Notion-Version", "2021-08-16");

        request.AddJsonBody(param);
        return request;
    }


    public async Task UpdatePageJSON(string page_id, string param, Action<string> callback)
    {
        var url = $"{urlPage}/{page_id}";
        var request = WebRequestWithParam(url, Method.Patch, param);
        var client = new RestClient();
        var t = await client.ExecuteAsync(request);
        callback(t.Content);
    }

My param is a json string:

string param = "{\"properties\": {\"In stock\": { \"checkbox\": true }}}";

When I use HttpWebRequest instead of RestSharp with the same parameter string, it works fine. Ref page: https://developers.notion.com/reference/patch-page

Any ideas ? Thanks


Solution

  • It's because AddJson is used to add objects that will be serialized to JSON by RestSharp.

    If you need to send a JSON string, use the AddStringBody:

    var request = new RestRequest(url, method).AddStringBody(param, "application/json");
    

    I would not recommend adding the content type header manually, it is completely unnecessary.