1

using RestSharp and RestSharp.Serializers.NewtonsoftJson (both 106.12.0), I am trying to make use of Confluence.

My content class:

 public class Content
    {        
        [JsonProperty(PropertyName = "MyId")]
        public int Id { get; set; }              
        [JsonProperty(PropertyName ="type")]
        [SerializeAs(Name ="type")]
        [JsonConverter(typeof(StringEnumConverter))]
        public ContentType Type { get; set; }
        public Space Space { get; set; }
        public string Title { get; set; }
        public Body Body { get; set; }
    }
}

The enum causing trouble:

    public enum ContentType
    {
        Unknown,
        Page,
        BlogPost
    }

The rest client is configured to use JSON by calling

restClient.UseSerializer(() => new JsonSerializer());          

Whenever I try to create a new Confluence page using this code...

public void CreatePage(Content content)
        {
            var request = new RestRequest("rest/api/content", Method.POST);                        
            request.AddJsonBody(content);
            
            IRestResponse<Content> response = RestClient.Post<Content>(request);
            if (Logger.IsDebugEnabled) Logger.Debug($"StatusCode: {response.StatusCode}");
            if (Logger.IsDebugEnabled) Logger.Debug($"ResponseStatus: {response.ResponseStatus}");
            if (Logger.IsDebugEnabled) Logger.Debug($"ErrorMessage: {response.ErrorMessage}");
            if (Logger.IsDebugEnabled) Logger.Debug($"Content: {response.Content}");            
        }

... I am getting a "badRequest" answer, response.content:

{"statusCode":400,"data":{"authorized":true,"valid":false,"errors":[{"message":{"key":"type is required to create content","args":[]}}],"successful":false},"message":"com.atlassian.confluence.api.service.exceptions.BadRequestException: Could not create content with type null"}

If i debug the request body value after the call, I can see the following:

{"Id":0,"Type":1,"Space":{"ID":null,"Key":"TESTSPACE2","Name":null,"Type":null,"Status":null},"Title":"FirstAutoPage","Body":{"Storage":{"Value":"\n <strong>This is a test!</strong>\n Did it work?"}}}

Obviously, none of my Content property attributes are used, neither the one for converting Id to "MyId" nor the ones for handling the ContentType enum (there still is "Type"=1, it should have been something like "type=Page".

So, what am I doing wrong?

Udontknow
  • 1,472
  • 12
  • 32
  • 2
    RestSharp doesn't use Json.NET by default, you need to use `RestSharp.Serializers.Newtonsoft.Json.NewtonsoftJsonSerializer` from [RestSharp.Serializers.NewtonsoftJson](https://www.nuget.org/packages/RestSharp.Serializers.NewtonsoftJson/) instead of `JsonSerializer`. See: [RestSharp deserialization of Enum Type Property](https://stackoverflow.com/a/35205826/3744182), [Deserialize JSON with RestSharp](https://stackoverflow.com/q/62486834/3744182), [RestSharp Serialize JSON in camelCase](https://stackoverflow.com/q/32867944/3744182). In fact this may be a duplicate, agree? – dbc Aug 08 '21 at 16:40
  • 1
    Thx, was able to infer now the right namespace. Indeed, i used the wrong component, I had to use "new RestSharp.Serializers.NewtonsoftJson.JsonNetSerializer()" instead. – Udontknow Aug 08 '21 at 18:50

0 Answers0