title: I want to call HDFS REST api to upload a file
body: <p>I want to call HDFS REST api to upload a file using <code>httplib</code>.</p>
  
  <p>My program created the file, but no content is in it.</p>
  
  <p>=====================================================</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import httplib
  
  conn=httplib.HTTPConnection("localhost:50070")
  conn.request("PUT","/webhdfs/v1/levi/4?op=CREATE")
  res=conn.getresponse()
  print res.status,res.reason
  conn.close()
  
  conn=httplib.HTTPConnection("localhost:50075")
  conn.connect()
  conn.putrequest("PUT","/webhdfs/v1/levi/4?op=CREATE&amp;user.name=levi")
  conn.endheaders()
  a_file=open("/home/levi/4","rb")
  a_file.seek(0)
  data=a_file.read()
  conn.send(data)
  res=conn.getresponse()
  print res.status,res.reason
  conn.close()
  </code></pre>
  
  <p>==================================================</p>
  
  <p>Here is the return:</p>
  
  <blockquote>
    <p>307 TEMPORARY_REDIRECT
    201 Created</p>
  </blockquote>
  
  <p>=========================================================</p>
  
  <p>OK, the file was created, but no content was sent.</p>
  
  <p>When I comment the <code>#conn.send(data)</code>, the result is the same, still no content.</p>
  
  <p>Maybe the file read or the send is wrong, not sure.</p>
  
  <p>Do you know how this happened?</p>