[英]Returning an HttpResponseMessage with a batch response
I've got a unit test which I'm trying to fix. 我有一个要修复的单元测试。 All I need to do is return a valid 200 HttpResponseMessage with a batch response for a single query (A 404 will do).
我需要做的就是返回一个有效的200 HttpResponseMessage,并带有针对单个查询的批处理响应(A 404会这样做)。 I'm new to OData and dealing with HTTPMessages in general.
我是OData的新手,通常会处理HTTPMessages。 This is what I've done so far, but I'm not sure it's the right way to do things.
到目前为止,这是我所做的事情,但是我不确定这是做事的正确方法。 Could you help me understand where I might be going wrong?
您能帮我了解我可能会出问题的地方吗?
string content = string.Format(
@"--batch_{0}
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 404 Not Found
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;charset=utf-8
Content-Length: 42",
batchCode);
content = content + Environment.NewLine + @"{ .error.:.not_found.,.reason.:.missing.}".
content = content + Environment.NewLine + Environment.NewLine + string.Format(@"--batch_{0}--", batchCode) + Environment.NewLine;
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(content, System.Text.Encoding.UTF8, "multipart/mixed")
};
--batchresponse_{batchCode}
. --batchresponse_{batchCode}
。 OData-Version
in the sub-responses, only in the header of the parent. OData-Version
,仅在父级的标头中指定即可。 HTTP/1.1 404 Not Found
line). HTTP/1.1 404 Not Found
行之前)。 A complete response body may look something like this: 完整的响应正文可能如下所示:
--batchresponse_4a21740c-169a-4442-b771-8989207e80e9
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
{"Some":"Json"}
--batchresponse_4a21740c-169a-4442-b771-8989207e80e9--
Also, the json in the response doesn't look like valid json to me, not sure if that's a problem. 另外,响应中的json对我来说看起来不是有效的json,不确定是否有问题。
string batchCode = this.GetBatchCode(requestContent);
var innerResponse = new HttpMessageContent(new HttpResponseMessage(HttpStatusCode.NotFound));
MultipartContent batchContent = new MultipartContent("mixed", "batch_" + batchCode);
innerResponse.Headers.Remove("Content-Type");
innerResponse.Headers.Add("Content-Type", "application/http");
innerResponse.Headers.Add("Content-Transfer-Encoding", "binary");
batchContent.Add(innerResponse);
var outerResponse = new HttpResponseMessage(HttpStatusCode.OK);
outerResponse.Content = batchContent;
return outerResponse;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.