2

Given the reposne of multiple HTTP request, I have to build a multipart response out of them. The problem I am faing is how to set the response headers of individual response in the final multipart response. For. eg.

HttpClient client = new HttpClient();
HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Get, "http://www.xyx/Service.svc/resource1");
HttpResponseMessage response1 = client.SendAsync(request).Result;

HttpRequestMessage request2 = new HttpRequestMessage(HttpMethod.Get, "http://www.xyx/Service.svc/resource2");
HttpResponseMessage response2 = client.SendAsync(request).Result;

MultipartContent content = new MultipartContent("mixed", "----Boundary");
content.Add(response1.Content);
content.Add(response2.Content);

The response I am getting from it is like:

------Boundary
Content-Length: 99427
Content-Type: application/json; charset=utf-8
Last-Modified: Mon, 20 Jan 2014 06:15:50 GMT

{"DebugInfo":null}
------Boundary
Content-Length: 99427
Content-Type: application/json; charset=utf-8
Last-Modified: Mon, 20 Jan 2014 06:15:50 GMT

{"DebugInfo":null}
------Boundary--

Now I want to include the reposne header of each request as part of individual response as well in the final response, and it should look like

HTTP/1.1 200 OK
Content-Type: multipart/mixed; boundary=batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 1972

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: <response-8a09ca85-8d1d-4f45-9eb0-da8e8b07ec83+1>

HTTP/1.1 200 OK
ETag: "lGaP-E0memYDumK16YuUDM_6Gf0/V43j6azD55CPRGb9b6uytDYl61Y"
Content-Type: application/json; charset=UTF-8
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 247

{
 "kind": "storage#objectAccessControl",
 "id": "example-bucket/obj1/allUsers",
 "selfLink": "https://www.googleapis.com/storage/v1beta2/b/example-bucket/o/obj1/acl/allUsers",
 "bucket": "example-bucket",
 "object": "obj1",
 "entity": "allUsers",
 "role": "READER"
}

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: 

HTTP/1.1 200 OK
ETag: "lGaP-E0memYDumK16YuUDM_6Gf0/91POdd-sxSAkJnS8Dm7wMxBSDKk"
Content-Type: application/json; charset=UTF-8
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 247

{
 "kind": "storage#objectAccessControl",
 "id": "example-bucket/obj2/allUsers",
 "selfLink": "https://www.googleapis.com/storage/v1beta2/b/example-bucket/o/obj2/acl/allUsers",
 "bucket": "example-bucket",
 "object": "obj2",
 "entity": "allUsers",
 "role": "READER"
}

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: 

HTTP/1.1 200 OK
ETag: "lGaP-E0memYDumK16YuUDM_6Gf0/d2Z1F1_ZVbB1dC0YKM9rX5VAgIQ"
Content-Type: application/json; charset=UTF-8
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 247

{
 "kind": "storage#objectAccessControl",
 "id": "example-bucket/obj3/allUsers",
 "selfLink": "https://www.googleapis.com/storage/v1beta2/b/example-bucket/o/obj3/acl/allUsers",
 "bucket": "example-bucket",
 "object": "obj3",
 "entity": "allUsers",
 "role": "READER"
}

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=--

Does anyone know how to add them in MultipartContent in C#?

1
  • How can we read multipart/mixed content, similar to your second example, from HttpResponseMessage object? Commented Jan 18, 2024 at 7:28

1 Answer 1

4

Got it. You can also add complete response to a MultipartContent as well.

MultipartContent content = new MultipartContent("mixed", "----Boundary");
content.Add(new HttpMessageContext(response1));
content.Add(new HttpMessageContext(response2));
Sign up to request clarification or add additional context in comments.

1 Comment

How can we read multipart/mixed content, similar to your second example, from HttpResponseMessage object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.