title: IPP over HTTP: getting 400 bad request as a response
body: <p>I'm on iOS and I'm trying to send a HTTP POST request to create a print job on a printer through a Mac which acts as a IPP server. I can correctly print using airprint, I'm trying now to go low-level because I need to send raw data to the printer. </p>
  
  <p>Something about my code:</p>
  
  <p>Everything is hardcoded. I'm still trying to figure out where I go wrong, so I'm just in a development/testing phase.
  The first part is about the definition of the binary structure of an IPP print-job request (and it should be right, according to the RFC 2910 specification).</p>
  
  <pre><code>char data[239] = {0x01, 0x01, // IPP version
      0x00, 0x04, // Print-job request
      0x00, 0x00, 0x00, 0x50, // Arbitrary request ID
      0x01, // Attribute group
      // ATT 1
      0x47, // charset value-tag
      0x00, 0x12, // name-length
      'a','t','t','r','i','b','u','t','e','s','-','c','h','a','r','s','e','t', // name
      0x00, 0x05, // value-length
      'u','t','f','-','8', // value
      // ATT 2
      0x48, // natural-language value-tag
      0x00, 0x1B, // name-length
      'a','t','t','r','i','b','u','t','e','s','-','n','a','t','u','r','a','l','-','l','a','n','g','u','a','g','e', // name                       
      0x00, 0x05, // value-length
      'e','n','-','u','s', // value
      // ATT 3
      0x45, // uri type value-tag
      0x00, 0x0B, // name-length
      'p','r','i','n','t','e','r','-','u','r','i', // name
      0x00, 0x47, // value-length
      'i','p','p',':','/','/','A','n','d','r','e','a','s','-','M','a','c','B','o','o','k','-','P','r','o','-','2','.','l','o','c','a','l','.',':','6','3','1','/','p','r','i','n','t','e','r','s','/','H','P','_','D','e','s','k','j','e','t','_','F','4','5','0','0','_','s','e','r','i','e','s', // value
      // ATT 4
      0x42, // requesting user id value-tag
      0x00, 0x14, // name-length
      'r','e','q','u','e','s','t','i','n','g','-','u','s','e','r','-','n','a','m','e', // name
      0x00, 0x05, // value-length
      'g','u','e','s','t', // value
      // ATT 5
      0x49, // document format value-tag
      0x00, 0x0f, // document format
      'd','o','c','u','m','e','n','t','-','f','o','r','m','a','t',
      0x18, // TODO
      'a','p','p','l','i','c','a','t','i','o','n','/','o','c','t','e','t','-','s','t','r','e','a','m',
      0x03, // end of attributes
      't','e','s','t'}; // data
  
  NSMutableData *printJob = [NSMutableData data];
  [printJob appendBytes:data length:sizeof(data)];
  NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [printJob length]];
  
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.23:631/printers/HP_Deskjet_F4500_series"]];
  [request setHTTPMethod:@"POST"];
  [request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
  [request setValue:@"application/ipp" forHTTPHeaderField:@"Content-Type"];
  [request setValue:@"Andreas-MacBook-Pro-2.local" forHTTPHeaderField:@"Host"];
  [request setValue:@"CUPS/1.5.0" forHTTPHeaderField:@"User-Agent"];
  [request setValue:@"100-continue" forHTTPHeaderField:@"Expect"];
  [request setHTTPBody:printJob];
  
  [[NSURLConnection connectionWithRequest:request delegate:self] start];
  </code></pre>
  
  <p>Whenever I run this code, after 10 seconds I get a HTTP 400 bad request response.</p>
  
  <p>The strange thing is that the HTTP request seems perfectly the same as the one I can capture with a packet analyzer whenever the airprint printing sends a print-job to the ipp server (and it works).</p>