Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the "type" parameter on a Web API MultiPartContent message?

I need to be able to set the Content-Type value for a multipart response to:

Content-Type: multipart/related; type=application/dicom; boundary={MessageBoundary}

This is defined in an international standard, so I have no choice in it, and I see that the use of "type=xxx/yyy" is standard usage to define the "main" content type in a MultiPart message.

BUT......I have not managed to find a way to produce this string using the otherwise excellent MultipartContent class in the web API. I've tried:

1) Doing nothing - hoping that it gets picked up from the first item in the content - it doesn't

2) Setting as part of the subtype - e.g.:

MultipartContent mpc = new MultipartContent("related; type=application/dicom+xml");

this fails with the error "The format of value 'multipart/related; type=application/dicom+xml' is invalid."

3) Setting explicitly as another parameter on the MediaTypeHeadervalue:

var mthv = new MediaTypeHeaderValue("multipart/related");
mthv.Parameters.Add(new NameValueHeaderValue("type", "application/dicom+xml"));

this gives the same error :-(

Am I missing something obvious here, or is there really no way to indicate the associated type?

UPDATE:

After some research, and decompiling the NameValueHeaderValue class, it seems that the problem is the / and + characters in the type value - as the constructor for NameValueHeaderValue checks both the name and the token to see whether their native and "token characters only" versions have the same length - i.e. whether they include any non-token characters - and reject them if that's the case. Given that the standard values for the type value are MIME types, which by definition include a / character, this does seem to be an overly and inappropriately strict constraint.

like image 336
medconn Avatar asked Mar 19 '23 13:03

medconn


1 Answers

You are correct in that the problem is in the / and + characters in the type value, but .NET is not entirely wrong in rejecting your input (I might wish that this part of the API were better engineered, but what do I know). RFC 2045, which specifies MIME, section 5.1, says that the parameter values must be quoted if they contain any of ()<>@,;:\"/[]?=. RFC 2387, which describes the multipart/related content type, says that the parameter values will usually require quoting and the example they give has the subordinate content type value properly quoted. As for your concrete problem, e.g. this ought to work:

var mpc = new MultipartContent ("related") ;
var nvv = new NameValueHeaderValue ("type", "\"application/dicom+xml\"") ;
mpc.Headers.ContentType.Parameters.Add (nvv) ;
like image 62
Anton Tykhyy Avatar answered Apr 26 '23 11:04

Anton Tykhyy