title: Create mime/multipart request containing multiple HTTP requests
body: <p>I am following <a href="http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx" rel="nofollow">this</a> tutorial for batching http requests with ASP.NET 4.5. I have the <a href="http://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/BatchSample/ReadMe.txt" rel="nofollow">sample</a> working, and now I need to write a client application in Python.</p>
  
  <p>This code creates, and sends a batch request to the web api:</p>
  
  <pre><code>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&lt;Customer&gt;(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&lt;Customer&gt;(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);
  </code></pre>
  
  <p>This is my attempt in Python but it doesn't work:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The request should look like this:</p>
  
  <pre><code>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--
  </code></pre>