ServiceStack在multipart / form-data请求中反序列化JSON内容

时间:2013-08-09 19:45:10

标签: rest servicestack multipartform-data

我正在使用ServiceStack创建RESTful服务,该服务应该使用带有multipart / form-data内容的POST。内容采用JSON格式,但是当我发送POST时,对象没有正确反序列化(所有属性都是null / default值)。如果我尝试将该对象作为常规POST(没有multipart / form-data)发送,则反序列化就好了。

我在ServiceStack代码中试图弄清楚发生了什么,这是我目前的理解:

  • HttpListenerRequestWrapper :: LoadMultiPart()正在加载多部分请求并将(非文件)部分保存到“FormData”,它将部件名称映射到其内容。但是,看起来内容类型(在解析各个部分时正确写入HttpMultiPart :: Element)会丢失,因为它没有存储在任何地方。
  • 稍后在控制流中,EndpointHandlerBase :: DeserializeHttpRequest()使用FormData和要反序列化的类型调用KeyValueDataContractDeserializer.Instance.Parse()。
  • 如果这是第一次反序列化该类型的对象,则会为该类型创建一个StringMapTypeDeserializer并缓存到typeStringMapSerializerMap。对于该类型的每个属性,我们调用JsvReader.GetParseFn()来获取ParseStringDelegate以解析该属性的反序列化。
  • 然后使用创建/缓存的StringMapTypeDeserializer来反序列化对象,使用之前设置的所有“ParseFn”...这些都将内容视为JSV格式。

我确认JsvReader.ParseFnCache中有很多类型,而JsonReader.ParseFnCache是​​空的。此外,如果我更改我的请求以删除所有引号(即将其从JSON转换为JSV格式),它会正确反序列化。一个奇怪的事情是我的对象的一个​​属性是一个Dictionary,并且正确地反序列化,即使它是JSON格式的;我假设这只是一个幸运的巧合(?!?)。

我理解这里发生的事情是否正确?这是ServiceStack中的已知限制吗?错误?除了将我的对象放在一个文件中并手动调用JsonSerializer.DeserializeFromStream()之外,还有解决方法吗?

谢谢!

JPS

此外,只是因为它很有用,这里是相关的请求和数据对象:

POST /api/Task HTTP/1.1
Accept: application/json
Content-Type: multipart/form-data; boundary=Boundary_1_1161035867_1375890821794
MIME-Version: 1.0
Host: localhost:12345
Content-Length: 385

--Boundary_1_1161035867_1375890821794
Content-Type: application/json
Content-Disposition: form-data; name="MyMap"

{"myfile.dat":"ImportantFile"}
--Boundary_1_1161035867_1375890821794
Content-Disposition: form-data; name="MyThing"
Content-Type: application/json

{"Id":123,"Name":"myteststring"}
--Boundary_1_1161035867_1375890821794
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="myfile.dat"

mydatagoeshere...
--Boundary_1_1161035867_1375890821794--

public class TestObj
{
    public long Id { get; set; }
    public string Name { get; set; }
}

[Route("/Task", "POST")]
public class TaskRequest : AuthenticatedRequest, IReturn<TaskResponse>
{
    public TestObj MyThing { get; set; }
    public Dictionary<string, string> MyMap { get; set; }
}

1 个答案:

答案 0 :(得分:1)

您是否尝试使用'ApiMember'属性设置请求对象的属性?特别是'ParameterType'属性。

/// <summary>
/// Create and upload a new video.
/// </summary>
[Api("Create and upload a new video.")]
[Route("/videos", "POST", Summary = @"Create and upload a new video.",
    Notes = "Video file / attachment must be uploaded using POST and 'multipart/form-data' encoding.")]
public class CreateVideo : OperationBase<IVideo>
{
    /// <summary>
    /// Gets or sets the video title.
    /// </summary>
    [ApiMember(Name = "Title",
        AllowMultiple = false,
        DataType = "string",
        Description = "Video title. Required, between 8 and 128 characters.",
        IsRequired = true,
        ParameterType = "form")]
    public string Title { get; set; }

    /// <summary>
    /// Gets or sets the video description.
    /// </summary>
    [ApiMember(Name = "Description",
        AllowMultiple = false,
        DataType = "string",
        Description = "Video description.",
        ParameterType = "form")]
    public string Description { get; set; }

    /// <summary>
    /// Gets or sets the publish date.
    /// </summary>
    /// <remarks>
    /// If blank, the video will be published immediately.
    /// </remarks>
    [ApiMember(Name = "PublishDate",
        AllowMultiple = false,
        DataType = "date",
        Description = "Publish date. If blank, the video will be published immediately.",
        ParameterType = "form")]
    public DateTime? PublishDate { get; set; }
}