我正在按照this教程学习使用ASP.NET 4.5进行批量HTTP请求。我已经成功运行了示例代码,现在需要用Python编写客户端应用程序。
以下代码创建并发送一个批量请求到Web API:
这是我在Python中的尝试,但它不起作用:
以下代码创建并发送一个批量请求到Web API:
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
//Create a request to query for customers
HttpRequestMessage queryCustomersRequest = new HttpRequestMessage(HttpMethod.Get, serviceUrl + "/Customers");
//Create a message to add a customer
HttpRequestMessage addCustomerRequest = new HttpRequestMessage(HttpMethod.Post, serviceUrl + "/Customers");
addCustomerRequest.Content = new ObjectContent<Customer>(addedCustomer, formatter);
//Create a message to update a customer
HttpRequestMessage updateCustomerRequest = new HttpRequestMessage(HttpMethod.Put, string.Format(serviceUrl + "/Customers/{0}", updatedCustomer.Id));
updateCustomerRequest.Content = new ObjectContent<Customer>(updatedCustomer, formatter);
//Create a message to remove a customer.
HttpRequestMessage removeCustomerRequest = new HttpRequestMessage(HttpMethod.Delete, string.Format(serviceUrl + "/Customers/{0}", removedCustomer.Id));
//Create the different parts of the multipart content
HttpMessageContent queryContent = new HttpMessageContent(queryCustomersRequest);
HttpMessageContent addCustomerContent = new HttpMessageContent(addCustomerRequest);
HttpMessageContent updateCustomerContent = new HttpMessageContent(updateCustomerRequest);
HttpMessageContent removeCustomerContent = new HttpMessageContent(removeCustomerRequest);
//Create the multipart/mixed message content
MultipartContent content = new MultipartContent("mixed", "batch_" + Guid.NewGuid().ToString());
content.Add(queryContent);
content.Add(addCustomerContent);
content.Add(updateCustomerContent);
content.Add(removeCustomerContent);
//Create the request to the batch service
HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, serviceUrl + "/batch");
//Associate the content with the message
batchRequest.Content = content;
//Send the message
HttpResponseMessage batchResponse = await client.SendAsync(batchRequest);
这是我在Python中的尝试,但它不起作用:
def build_request( url, type, headers, data = {}):
#Build appropriate request
if type == "get":
request = Request('GET', url, headers=headers)
elif type == "post":
request = Request('POST', url, data = json.dumps(data), headers = {'Content-Type':'application/json'})
elif type == "delete":
request = Request('DELETE', url, headers = {'Content-Type':'application/json'})
elif type == "put":
request = Request('PUT', url, data = json.dumps(data), headers = {'Content-Type':'application/json'})
elif type == "patch":
request = Request('PATCH', url, data = json.dumps(data), headers = {'Content-Type':'application/json'})
prepared_request = request.prepare()
return prepared_request
#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)
request_list = [get_customers,add_customer,update_customer, remove_customer]
batch_request = requests.Request('POST',url + "/batch", data=request_list)
s = Session()
batch_request.prepare()
resp = s.send(batch_request)
请求应该看起来像这样:
POST http://localhost:12345/api/batch HTTP/1.1
Content-Type: multipart/mixed; boundary="batch_357647d1-a6b5-4e6a-aa73-edfc88d8866e"
Host: localhost:12345
Content-Length: 857
Expect: 100-continue
--batch_357647d1-a6b5-4e6a-aa73-edfc88d8866e
Content-Type: application/http; msgtype=request
GET /api/WebCustomers HTTP/1.1
Host: localhost:13245
--batch_357647d1-a6b5-4e6a-aa73-edfc88d8866e
Content-Type: application/http; msgtype=request
POST /api/WebCustomers HTTP/1.1
Host: localhost:13245
Content-Type: application/json; charset=utf-8
{"Id":129,"Name":"Name4752cbf0-e365-43c3-aa8d-1bbc8429dbf8"}
--batch_357647d1-a6b5-4e6a-aa73-edfc88d8866e
Content-Type: application/http; msgtype=request
PUT /api/WebCustomers/1 HTTP/1.1
Host: localhost:13245
Content-Type: application/json; charset=utf-8
{"Id":1,"Name":"Peter"}
--batch_357647d1-a6b5-4e6a-aa73-edfc88d8866e
Content-Type: application/http; msgtype=request
DELETE /api/WebCustomers/2 HTTP/1.1
Host: localhost:13245
--batch_357647d1-a6b5-4e6a-aa73-edfc88d8866e--