# {Invalid NEST response built from a successful (200) low level call on POST: /\_bulk}

**URL:** <https://discuss.elastic.co/t/invalid-nest-response-built-from-a-successful-200-low-level-call-on-post-bulk/338042>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [July 10, 2023, 7:45pm UTC](https://discuss.elastic.co/t/invalid-nest-response-built-from-a-successful-200-low-level-call-on-post-bulk/338042 "2023-07-10T19:45:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janderson\_Goncalves](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/janderson_goncalves/32/122308_2.png) [@Janderson\_Goncalves](https://discuss.elastic.co/u/Janderson_Goncalves)\
**Post date:** [July 10, 2023, 7:46pm UTC](https://discuss.elastic.co/t/invalid-nest-response-built-from-a-successful-200-low-level-call-on-post-bulk/338042/1 "2023-07-10T19:46:00Z")

</div>

I am trying to create a Middleware that captures the Request and Response of a given application and indexes it in Elastic. But the elastic always returns the following error and already when debugging is going an object with data correctly, not a Json array, Help me!

Below my Middleware

```auto
public class ElasticsearchMiddleware
{
    private readonly RequestDelegate _next;

    public ElasticsearchMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context, IElasticsearchService elasticsearch)
    {
        await _next(context);

        var requestObj = await GetBodyAsync(context.Request.Body);
        var responseObj = await GetBodyAsync(context.Response.Body);

        var controllerActionDescriptor = context.Features.Get<IEndpointFeature>()?.Endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>();

        var requestType = GetRequestType(controllerActionDescriptor);
        var indexName = requestType?.Name.ToLower();

        elasticsearch.IndexRequestResponse(requestObj, responseObj, indexName);
    }

    private async Task<object> GetBodyAsync(Stream bodyStream)
    {
        bodyStream.Seek(0, SeekOrigin.Begin);
        using (StreamReader reader = new StreamReader(bodyStream, Encoding.UTF8))
        {
            string body = await reader.ReadToEndAsync();
            return JsonConvert.DeserializeObject(body);
        }
    }

    private Type GetRequestType(ControllerActionDescriptor actionDescriptor)
    {
        var parameters = actionDescriptor?.MethodInfo.GetParameters();
        var requestParameter = parameters?.FirstOrDefault();
        return requestParameter?.ParameterType;
    }
}

```

Here my service class

```auto
public class ElasticsearchService : IElasticsearchService
{
    private readonly ElasticClient _elasticClient;

    public ElasticsearchService(IOptions<ElasticConfiguration> elasticsearchSettings)
    {
        var uri = new Uri(elasticsearchSettings.Value.Uri);
        var username = elasticsearchSettings.Value.Username;
        var password = elasticsearchSettings.Value.Password;
        var connectionSettings = new ConnectionSettings(uri)
            .DefaultIndex(elasticsearchSettings.Value.DefaultIndex)
            .BasicAuthentication(username, password);
        _elasticClient = new ElasticClient(connectionSettings);
    }

    public void IndexRequestResponse(object request, object response, string indexName)
    {
        if (!_elasticClient.Indices.Exists(indexName.ToLower()).Exists)
            _elasticClient.Indices.Create(indexName.ToLower());

        var bulkDescriptor = new BulkDescriptor();
        bulkDescriptor.Index<object>(idx => idx.Document(request).Index(indexName));
        bulkDescriptor.Index<object>(idx => idx.Document(response).Index(indexName));

        var bulkResponse = _elasticClient.Bulk(bulkDescriptor);

        if (bulkResponse != null && !bulkResponse.IsValid)
        {
            if (bulkResponse.OriginalException != null)
            {
                throw new Exception(bulkResponse.OriginalException.ToString());
            }
            else
            {
                throw new Exception("Unknown bulk response error.");
            }
        }
    }

}

```

---

<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:** [August 7, 2023, 7:46pm UTC](https://discuss.elastic.co/t/invalid-nest-response-built-from-a-successful-200-low-level-call-on-post-bulk/338042/2 "2023-08-07T19:46:00Z")

</div>

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