title: Sending data Curl/Json in Python
body: <p>I`m trying to make those 2  requests in python:</p>
  
  <p>Request 1: </p>
  
  <pre><code> curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth1", "widget":   "id1", "title": "Something1",  "text": "Some text", "moreinfo": "Subtitle" }'   serverip
  </code></pre>
  
  <p>Request 2:</p>
  
  <pre><code> vsphere_dict = {}
   vsphere_dict['server_name'] = "servername"
   vsphere_dict['api_version'] = apiVersion
   vsphere_dict['guest_count'] = guestCount
   vsphere_dict['guest_on']    = guestOnLen
   vsphere_dict['guest_off']   = guestOffLen
  
   #Convert output to Json to be sent
   data = json.dumps(vsphere_dict)
  
   curl -X POST -H "Content-Type: application/json" -d 'data' serverip
  </code></pre>
  
  <p>Neither of them seems to work. Is there any way I can send them in Python?</p>
  
  <p>Update:</p>
  
  <p>The part that I cannot handle is the pass auth and widget. I have tried the following without success:</p>
  
  <pre><code>import urllib2
  import urllib
  
  vsphere_dict = dict(
      server_name="servername",
      api_version="apiVersion",
      guest_count="guestCount",
      guest_on="guestOnLen",
      guest_off="guestOffLen",
  )
  
  url = "http://ip:port"
  
  auth = "authid89"
  widget = "widgetid1"
  
  # create request object, set url and post data
  req = urllib2.Request(auth,url, data=urllib.urlencode(vsphere_dict))
  # set header
  req.add_header('Content-Type', 'application/json')
  # send request
  response = urllib2.urlopen(req)**
  </code></pre>
  
  <p>Resulting in "urllib2.HTTPError: HTTP Error 500: Internal Server Error"</p>
  
  <p>Any ideas how I can pass the auth and widget correctly?</p>
  
  <p>UPDATE:</p>
  
  <p>To see what is different I have started a nc server locally. Here are the results:</p>
  
  <p>Correct curl request using this code:</p>
  
  <pre><code> curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth", "widget": "widgetid", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://localhost:8123
  </code></pre>
  
  <p>sends this <strong>which does work:</strong></p>
  
  <pre><code> POST / HTTP/1.1
   User-Agent: curl/7.21.0 (i386-redhat-linux-gnu) libcurl/7.21.0 NSS/3.12.10.0 zlib/1.2.5  libidn/1.18 libssh2/1.2.4
   Host: localhst:8123
   Accept: */*
   Content-Type: application/json
   Content-Length: 165
  
   { "auth_token": "token", "widget": "widgetid", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }
  </code></pre>
  
  <p>And request using this code </p>
  
  <pre><code>  import requests
    import simplejson as json
  
    url = "http://localhost:8123"
    data = {'auth_token': 'auth1', 'widget': 'id1', 'title': 'Something1', 'text': 'Some   text', 'moreinfo': 'Subtitle'}
    headers = {'Content-type': 'application/json'}
    r = requests.post(url, data=json.dumps(data), headers=headers)
  </code></pre>
  
  <p>sends this which <strong>does not work:</strong></p>
  
  <pre><code> POST / HTTP/1.1
   Host: localhst:8123
   Content-Length: 108
   Content-type: application/json
   Accept-Encoding: gzip, deflate, compress
   Accept: */*
   User-Agent: python-requests/2.0.1 CPython/2.7.0 Linux/2.6.35.14-106.fc14.i686
  
   {"text": "Some text", "auth_token": "auth1", "moreinfo": "Subtitle", "widget": "id1",  "title": "Something1"}
  </code></pre>