# Issue deserializing Nest.BulkResponse returned by Rest service

**URL:** <https://discuss.elastic.co/t/issue-deserializing-nest-bulkresponse-returned-by-rest-service/76329>\
**Category:** Elasticsearch\
**Created:** [February 24, 2017, 1:38am UTC](https://discuss.elastic.co/t/issue-deserializing-nest-bulkresponse-returned-by-rest-service/76329 "2017-02-24T01:38:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RajPwC](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@RajPwC](https://discuss.elastic.co/u/RajPwC)\
**Post date:** [February 24, 2017, 1:38am UTC](https://discuss.elastic.co/t/issue-deserializing-nest-bulkresponse-returned-by-rest-service/76329/1 "2017-02-24T01:38:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gmarz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gmarz/32/22178_2.png) [@gmarz](https://discuss.elastic.co/u/gmarz)\
**Post date:** [February 27, 2017, 3:37pm UTC](https://discuss.elastic.co/t/issue-deserializing-nest-bulkresponse-returned-by-rest-service/76329/2 "2017-02-27T15:37:21Z")

</div>

Deserializing `BulkResponse` is not straightforward and requires a [custom converter](https://github.com/elastic/elasticsearch-net/blob/master/src/Nest/Document/Multiple/Bulk/BulkResponseItem/BulkResponseItemJsonConverter.cs).

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](https://github.com/elastic/elasticsearch-net/blob/master/src/Elasticsearch.Net/Connection/HttpConnection.cs).

For guidance, take a look at [our .NET core HttpConnection](https://github.com/elastic/elasticsearch-net/blob/master/src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs) which uses `HttpClient`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 27, 2017, 3:37pm UTC](https://discuss.elastic.co/t/issue-deserializing-nest-bulkresponse-returned-by-rest-service/76329/3 "2017-03-27T15:37:32Z")

</div>

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