title: upload file to django server using curl
body: <p>On Django python server, I have customized a URL where users can upload files. Now, problem is that I am successfully able to upload files when I hit the browser but when I try same thing using curl, I am not able to do so. </p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>import json
  
  from django.http import HttpResponse
  from django.template import Context, RequestContext
  from django.shortcuts import render_to_response, get_object_or_404
  
  # -*- coding: utf-8 -*-
  from django.http import HttpResponseRedirect
  from django.core.urlresolvers import reverse
  
  from sdm.models import Document
  from sdm.forms import DocumentForm
  
  def lists(request):
     # Handle file upload
     if request.method == 'POST':
       form = DocumentForm(request.POST, request.FILES)
       if form.is_valid():
          newdoc = Document(docfile = request.FILES['docfile'])
          newdoc.save()
  
          # Redirect to the document list after POST
          return HttpResponseRedirect(reverse('sdm:lists'))
  
  else:
      form = DocumentForm() # A empty, unbound form
  
  # Load documents for the list page
  documents = Document.objects.all()
  
  # Render list page with the documents and the form
  return render_to_response(
      'sdm/lists.html',
      {'documents': documents, 'form': form},
      context_instance=RequestContext(request)
  )
  </code></pre>
  
  <p>........
     ........
     ........
     ........</p>
  
  <p><strong>lists.html</strong></p>
  
  <pre><code> &lt;!DOCTYPE html&gt;
   &lt;html&gt;
   &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Minimal Django File Upload Example&lt;/title&gt;   
   &lt;/head&gt;
   &lt;body&gt;
   &lt;!-- List of uploaded documents --&gt;
   {% if documents %}
      &lt;ul&gt;
      {% for document in documents %}
          &lt;li&gt;&lt;a href="{{document.docfile.url }}"&gt;{{ document.docfile.name }}&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
      &lt;/ul&gt;
   {% else %}
      &lt;p&gt;No documents.&lt;/p&gt;
   {% endif %}
  
      &lt;!-- Upload form. Note enctype attribute! --&gt;
      &lt;form action="{% url sdm:lists %}" method="post" enctype="multipart/form-data"&gt;
          {% csrf_token %}
          &lt;p&gt;{{form.non_field_errors }}&lt;/p&gt;
          &lt;p&gt;{{form.docfile.label_tag }} {{form.docfile.help_text }}&lt;/p&gt;
          &lt;p&gt;
              {{ form.docfile.errors }}
              {{ form.docfile }}
          &lt;/p&gt;
          &lt;p&gt;&lt;input type="submit" name="press" value="Upload" /&gt;&lt;/p&gt;
      &lt;/form&gt;
   &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p><strong>On Browser</strong></p>
  
  <p><img src="https://i.stack.imgur.com/ihKYO.png" alt="enter image description here"></p>
  
  <p><strong>On Terminal, I tried</strong></p>
  
  <pre><code> $ curl --request PUT --upload-file filename http://wings.spectrumserver/sdm/lists
  
   $ curl --form upload-file=filename  --form press=Upload 
  
   http:// wings. spectrumserver/sdm/lists
  
  $ curl --upload-file filename http://wings.spectrumserver/sdm/lists
  $ curl --upload-file filename press=upload http://wings.spectrumserver/sdm/lists
  
  $ curl -H 'Expect:' -F data=@filename -F submit=Upload wings.spectrumserver/sdm/lists
  
  // In all cases, No error but no file upload
  </code></pre>
  
  <p>I tried some other variations but nothing seems working out. Also some other commands I tried which gives "NO csrf token error". I also tried removing <code>csrf token</code> entries form <code>html file</code> and <code>setting.py</code> but nothing worked.</p>
  
  <p>I am all new to curl and python both. Main purpose is to upload file using some python script. I thought if I can upload through curl then same things can be replicated in python script with curl library so if this is not working out then can anyone suggest some python code to upload files to this server.       </p>
  
  <p><strong>Edit :</strong></p>
  
  <pre><code>$ curl -i -F name=press -F f13 wings.spectrumserver/sdm/lists
  Warning: Illegally formatted input field!
  curl: option -F: is badly used here
  curl: try 'curl --help' or 'curl --manual' for more information
  </code></pre>
  
  <p><strong>Edit2- Header Response (f13 is new file thats not included)</strong></p>
  
  <pre><code>$ curl -i http://wings.spectrumserver/sdm/lists
  </code></pre>
  
  <p>HTTP/1.1 200 OK<br>
     Date: Thu, 07 Nov 2013 23:19:18 GMT
     Server: Apache/2.2.22 (Ubuntu)<br>
     Vary: Accept-Encoding
     Content-Length: 1263
     Content-Type: text/html; charset=utf-8</p>
  
  <p>
     
       
          
          Minimal Django File Upload Example<br>
      
      
      </p>
  
  <pre><code>    &lt;ul&gt;
  
          &lt;li&gt;&lt;a href="/media/documents/2013/10/28/templates.zip"&gt;documents/2013/10
      /28/templates.zip&lt;/a&gt;&lt;/li&gt;
  
          &lt;li&gt;&lt;a href="/media/documents/2013/11/07/list"&gt;documents/2013/11/07/list&lt;/a&gt;
      &lt;/li&gt;
  
          &lt;li&gt;&lt;a href="/media/documents/2013/11/07/f1"&gt;documents/2013/11/07/f1&lt;/a&gt;&lt;/li&gt;
  
          &lt;li&gt;&lt;a href="/media/documents/2013/11/07/f12"&gt;documents/2013/11/07/f12&lt;/a&gt;&lt;/li&gt;
  
          &lt;li&gt;&lt;a href="/media/documents/2013/11/07/hello.html"&gt;documents/2013/11
          /07/hello.html&lt;/a&gt;&lt;/li&gt;
  
      &lt;/ul&gt;
  
  
      &lt;!-- Upload form. Note enctype attribute! --&gt;
      &lt;form action="/sdm/lists" method="post" enctype="multipart/form-data"&gt;
  
     &lt;!--            
     --&gt;        &lt;p&gt;&lt;/p&gt;
          &lt;p&gt;&lt;label for="id_docfile"&gt;Select a file&lt;/label&gt; max. 42 megabytes&lt;/p&gt;
          &lt;p&gt;
  
              &lt;input type="file" name="docfile" id="id_docfile" /&gt;
          &lt;/p&gt;
          &lt;p&gt;&lt;input type="submit" name="press" value="Upload" /&gt;&lt;/p&gt;
      &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>