Issue deserializing Nest.BulkResponse returned by Rest service

Hello,

I am getting error when deserializing BulkResponse returned by a REST call on the client side.

"Additional text found in JSON string after finishing deserializing object."

Client and Service are quite straight forward currently.

public class ElasticSearchIndexerClient : BaseClient
{
    public BulkResponse AddDocuments(Guid indexName, List<ESDocument> esDocumentList)
    {
        var response = Post(esDocumentList, routePrefix + "addDocuments/" + indexName.ToString());
        BulkResponse resp = GetResponse<BulkResponse>(response);

        return resp;
    }

    public static T GetResponse<T>(HttpResponseMessage httpResponse) where T : IResponse
    {
        try
        {
            var content = httpResponse.Content.ReadAsStringAsync().Result;
            **T response = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(content);**
            return response;
        }
        catch (Exception)
        {
            return default(T);
        }
    }
}

I have a rest service to control writes to ElasticSearch as shown below:

[RoutePrefix("api/indexer")]
public class IndexerController : ApiController
{
    public ElasticSearchRepositoryWriter ESRepoWriter { get; private set; }

    [Route("addDocuments/{indexName}"), HttpPost]
    public Nest.BulkResponse AddDocuments(Guid indexName, [FromBody]List<ESDocument> esDocumentList)
    {
        ESRepoWriter.SetIndex(indexName);
        var response = ESRepoWriter.AddDocumentss(esDocumentList);

        return response as BulkResponse;
    }
}

Service uses JsonFormatter, so both sides are using Newtonsoft.Json. In debug mode, I can see that the returned json content is correct.

Please advise how to resolve the issue. Is it not ok to return Nest.BulkResponse and other Nest objects?

Thanks in advance.

Regards,
Raj

Deserializing BulkResponse is not straightforward and requires a custom converter.

Is there a reason you're handling the request/response and deserialization yourself rather than going through the client itself? If it's because you want to use HttpClient, then the best way to go about that is creating your own custom connection implementation by inheriting from HttpConection.

For guidance, take a look at our .NET core HttpConnection which uses HttpClient.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.