Flurl: UseNewtonsoft + clientless pattern, default serializer incorrectly used to serialize request body

Using a complex JSON object - something like the following, the object is incorrectly serialized by PostJsonAsync whether using STJ or Json.Net. However, when serialized outside PostJsonAsync using default settings for Json.Net, the result is as expected.

Using latest 4pre6.

  var _linkItems = new
  {
      Int_Key = 0,
      Int_Box = 1111,
      Int_Link = 2222
  };
  dynamic _linkWrapper = new JObject();
  _linkWrapper["0"] = JObject.FromObject(_linkItems);
  var _params = new
  {
      pageToke = "",
      lotIntKey = 11,
      module = 2,
      lotData = new
      {
          Details = (string)null,
          Custom = (string)null,
          LinkItems = _linkWrapper,
          Security = (string)null,
          BIMLinks = (string)null
      }
  };

E.g this doesn’t work:

            var path = lot_Segments.Append("MyMethod");
            var _req = _cookieSession.Request(path.ToArray());
            var _postReq = await _req
                .PostJsonAsync(_params);

However, this does:

            var json = JsonConvert.SerializeObject(_params);
            var content = new CapturedJsonContent(json);
            var _postReq = await _req
                .SendAsync(HttpMethod.Post, content);

The latter correctly sends the serialized string as {“pageToke”:“”,“lotIntKey”:11,“module”:2,“lotData”:{“Details”:null,“Custom”:null,“LinkItems”:{“0”:{“Int_Key”:0,“Int_Box”:1111,“Int_Link”:2222}},“Security”:null,“BIMLinks”:null}}

The first one mangles the content of linkitems into some strange set of [[[],[],[]][[]]] etc.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I see you’re using 4.0. Newtonsoft was removed so JObject is no longer supported. Neither are dynamics. Try the just-released Flurl.Http.Newtonsoft companion lib. I’m in the middle of documentation updates now, but here’s the quick & dirty of how to use:

  • Option 1, specific client: flurlClient.Settings.JsonSerializer = new NewtonsoftJsonSerializer();
  • Option 2, global: FlurlHttp.Clients.UseNewtonsoft();

If you’re already well versed in #770 (most people probably aren’t), UseNewtonsoft() is also available on IFlurlClientBuilder when configuring a specific client at startup.