我尝试使用mime / multipart请求构建一个由多个子请求组成的http请求。这是我的客户代码:
def build_request( url, type, headers, data = {}):
request = MIMEBase("application", "http")
#Build appropriate request
if type == "get":
request.data = Request('GET', url, headers=headers)
elif type == "post":
request.data = Request('POST', url, data = json.dumps(data), headers = {'Content-Type':'application/http'})
elif type == "delete":
request.data = Request('DELETE', url, headers = {'Content-Type':'application/http'})
elif type == "put":
request.data = Request('PUT', url, data = json.dumps(data), headers = {'Content-Type':'application/http'})
elif type == "patch":
request.data = Request('PATCH', url, data = json.dumps(data), headers = {'Content-Type':'application/http'})
return request
if __name__ == '__main__':
url = "http://localhost:65200/api";
#Get customers
get_customers = build_request( url + "/Customers", "get", headers)
#Add a customer
add_customer = build_request( url + "/Customers", "post", data=added_customer, headers=headers)
#update a customer
update_customer = build_request( url + "/Customers/{0}".format(updated_customer["Id"]), "put", data=updated_customer, headers=headers)
#Remove a customer
remove_customer = build_request( url + "/Customers/{0}".format(removed_customer["Id"]), "delete", headers=headers)
s = Session()
uuid = uuid.uuid4()
message = MIMEMultipart("mixed", "batch_{0}".format(uuid), [get_customers,add_customer,update_customer,remove_customer])
headers = {'Content-Type':'multipart/mixed; boundary=\"{0}\"'.format(uuid)}
#Error here
reply = requests.post(url + "/batch", data = message.as_string(), headers=headers)
错误:
' {"消息":"发生错误。"," ExceptionMessage":"意外 MIME多部分流的结尾。 MIME多部分消息不是 。完整"" ExceptionType":" System.IO.IOException""堆栈跟踪":"在 System.Net.Http.Formatting.Parsers.MimeMultipartBodyPartParser.d__0.MoveNext(个)\ r \ n 在 System.Net.Http.HttpContentMultipartExtensions.d__8.MoveNext(个)\ r \ n --- 从抛出异常的先前位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n at System.Net.Http.HttpContentMultipartExtensions.d__0
1.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\ r \ n at System.Web.Http.Batch.DefaultHttpBatchHandler.d__13.MoveNext(个)\ r \ n --- 从抛出异常的先前位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\\r\\n at System.Web.Http.Batch.DefaultHttpBatchHandler.<ProcessBatchAsync>d__1.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\ r \ n at System.Web.Http.Batch.HttpBatchHandler.d__0.MoveNext()&#34;}&#39;
有人知道如何解决这个问题吗?
捕获的提琴手会话:
POST http://localhost:65200/api/batch HTTP/1.1
Host: localhost:65200
Content-Length: 535
Content-Type: multipart/mixed; boundary="7ef5c2df-3aa4-4e8c-bb47-8c9105bc3457"
Accept-Encoding: gzip, deflate, compress
Accept: */*
User-Agent: python-requests/2.1.0 CPython/2.7.6 Windows/7
Content-Type: multipart/mixed;
boundary="batch_7ef5c2df-3aa4-4e8c-bb47-8c9105bc3457"
MIME-Version: 1.0
--batch_7ef5c2df-3aa4-4e8c-bb47-8c9105bc3457
Content-Type: application/http
MIME-Version: 1.0
--batch_7ef5c2df-3aa4-4e8c-bb47-8c9105bc3457
Content-Type: application/http
MIME-Version: 1.0
--batch_7ef5c2df-3aa4-4e8c-bb47-8c9105bc3457
Content-Type: application/http
MIME-Version: 1.0
--batch_7ef5c2df-3aa4-4e8c-bb47-8c9105bc3457
Content-Type: application/http
MIME-Version: 1.0
--batch_7ef5c2df-3aa4-4e8c-bb47-8c9105bc3457--
HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcbXJ5YW4uQVNBVklFXERvY3VtZW50c1xWaXN1YWwgU3R1ZGlvIDIwMTNcUHJvamVjdHNcQmF0Y2hTYW1wbGVcQmF0Y2hTYW1wbGVcYXBpXGJhdGNo?=
X-Powered-By: ASP.NET
Date: Mon, 28 Apr 2014 15:31:48 GMT
Content-Length: 2075
{"Message":"An error has occurred.","ExceptionMessage":"Unexpected end of MIME multipart stream. MIME multipart message is not complete.","ExceptionType":"System.IO.IOException","StackTrace":" at System.Net.Http.Formatting.Parsers.MimeMultipartBodyPartParser.<ParseBuffer>d__0.MoveNext()\r\n at System.Net.Http.HttpContentMultipartExtensions.<MultipartReadAsync>d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at System.Net.Http.HttpContentMultipartExtensions.<ReadAsMultipartAsync>d__0`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Batch.DefaultHttpBatchHandler.<ParseBatchRequestsAsync>d__13.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Batch.DefaultHttpBatchHandler.<ProcessBatchAsync>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Batch.HttpBatchHandler.<SendAsync>d__0.MoveNext()"}