OData / OData/AspNetCoreOData

Error reading multipart Batch Response

Open
#1,276 6 comments 0 reactions 1 assignee View on GitHub

@gathogojr is already working on this.

Since Jul 16, 2024.

bug
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Hello, I am trying to build an OData server using Microsoft.AspnetCore.OData.
I have a problem when I try to send a Batch request with a multipart request. The problem seems happend in the response.
In this repository, https://github.com/GLuca74/TestOData, there is a solution with 3 projects :

TestODataServer - The server
TestODataClient - The client
TestODataModels - A shared Entity

the client request is processed by the server and it is able to receive the response, but when I try to read the actual response I get an error.
To read the response I use Microsoft.AspNet.WebApi.Client.
This is the code :

                using (var response = await httpClient.SendAsync(request).ConfigureAwait(false))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        HttpRequestException ex = new HttpRequestException(await response.Content.ReadAsStringAsync(), null, response.StatusCode);
                        throw ex;
                    }
                    var multipart = await response.Content.ReadAsMultipartAsync(); 
                    foreach (var c in multipart.Contents)
                    {
                        var responses = await c.ReadAsMultipartAsync();
                        foreach (var r in responses.Contents)
                        {
                            var xx1 = await r.ReadAsHttpResponseMessageAsync(); //here i get an exception
                        }

                    }
                }

The exception is :

System.ArgumentException
  HResult=0x80070057
  Messaggio=Invalid 'HttpContent' instance provided. It does not have a content type header with a value of 'application/http; msgtype=response'. Arg_ParamName_Name
  Origine=System.Net.Http.Formatting
  Analisi dello stack:
   in System.Net.Http.HttpMessageContent.ValidateHttpMessageContent(HttpContent content, Boolean isRequest, Boolean throwOnError)
   in System.Net.Http.HttpContentMessageExtensions.ReadAsHttpResponseMessageAsync(HttpContent content, Int32 bufferSize, Int32 maxHeaderSize, CancellationToken cancellationToken)
   in System.Net.Http.HttpContentMessageExtensions.ReadAsHttpResponseMessageAsync(HttpContent content, Int32 bufferSize, Int32 maxHeaderSize)
   in System.Net.Http.HttpContentMessageExtensions.ReadAsHttpResponseMessageAsync(HttpContent content, Int32 bufferSize)
   in System.Net.Http.HttpContentMessageExtensions.ReadAsHttpResponseMessageAsync(HttpContent content)
   in TestODataClient.Program.<Main>d__0.MoveNext() in D:\VM\TestOData\TestODataClient\Program.cs: riga 75

sniffing request and response with Fiddler, i have this :

POST https://localhost:7200/$batch HTTP/1.1
Host: localhost:7200
OData-Version: 4.0
OData-MaxVersion: 4.0
Accept: multipart/mixed
Accept-Charset: UTF-8
Connection: Keep-Alive
Content-Type: multipart/mixed; boundary=batch_3a22c121-57c7-49d9-9cd5-3f6605bb80c6
Content-Length: 639

--batch_3a22c121-57c7-49d9-9cd5-3f6605bb80c6
Content-Type: multipart/mixed; boundary=changeset_1f9fece3-486f-4279-a568-8fffe53f3653

--changeset_1f9fece3-486f-4279-a568-8fffe53f3653
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST /Cities HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Content-Type: application/json; odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8

{"CityID":"bad222ce-f863-4647-ba4c-c0929eafb971","CityName":"Madrid"}
--changeset_1f9fece3-486f-4279-a568-8fffe53f3653--

--batch_3a22c121-57c7-49d9-9cd5-3f6605bb80c6--

HTTP/1.1 200 OK
Content-Type: multipart/mixed;boundary=batchresponse_d96b63e0-77dd-41aa-b3ac-0e9d6cb38562
Date: Thu, 11 Jul 2024 14:07:49 GMT
Server: Kestrel
Transfer-Encoding: chunked
OData-Version: 4.0

1fe
--batchresponse_d96b63e0-77dd-41aa-b3ac-0e9d6cb38562
Content-Type: multipart/mixed; boundary=changesetresponse_631283f4-b5fd-48b4-8e9e-4244ca32cb53

--changesetresponse_631283f4-b5fd-48b4-8e9e-4244ca32cb53
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

HTTP/1.1 201 Created
Location: https://localhost:7200/Cities(bad222ce-f863-4647-ba4c-c0929eafb971)
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; charset=utf-8
OData-Version: 4.0


fe
{"@odata.context":"https://localhost:7200/$metadata#Cities/$entity","CityID":"bad222ce-f863-4647-ba4c-c0929eafb971","CityName":"Madrid"}
--changesetresponse_631283f4-b5fd-48b4-8e9e-4244ca32cb53--
--batchresponse_d96b63e0-77dd-41aa-b3ac-0e9d6cb38562--

0

It seems that ReadAsHttpResponseMessageAsync not like this

--changesetresponse_631283f4-b5fd-48b4-8e9e-4244ca32cb53
Content-Type: application/http // msgtype=response seems missing

I tryed in a custom ODataBatchHandler to found a way to add msgtype=response to the response but i wanst able to found if is possible and where

I also tryed to use Microsoft.OData.Client and it works without problems, but I can not use this library.

Can you help me found a solution?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.