title: python GET and POST with json and variables
body: <p>get parse post </p>
  
  <p>I can't seem to get this to work. This is what I think is happening: (But obviously I am doing something wrong.)</p>
  
  <ol>
  <li>User gives me in the form of nid.</li>
  <li>Take nid and construct url to GET json dump.</li>
  <li>Take value of title and body from json dump and store in variables title and body.</li>
  <li>Construct another url with the various variables and POST to site 2.</li>
  </ol>
  
  <p>Attempt:</p>
  
  <pre><code>import requests
  import json
  
  #Token used to post
  auth_token = "X"
  
  #Take and parse payload for title and body at site 1.
  def payload(nid):
      from urllib.request import urlopen
      with urlopen("www.site1.com/" + nid + ".json") as rr:
          result = json.loads(rr.read().decode(rr.headers.get_content_charset("utf-8")))
      title = (result["title"])
      body = (result["body"]["und"])
  
  #Take title, body, auth_token and then also nid from user input, and POSTing  to site 2 
  def add(nid):
      url = "www.site2.com/stuff.json"
      headers = {"content-type": "application/json"}
      payload = {
          "auth_token": auth_token,
          "document":
              {
                  "external_id": nid,
                  "fields": [
                      {"name": "title", "value": title, "type": "string"},
                      {"name": "path", "value": "www.site1.com/node/"+nid,"type": "enum"},
                      {"name": "nid", "value": nid, "type": "integer"},
                      {"name": "body", "value": body, "type": "text"},
                  ]}
      }
  
      r = requests.post(url, data=json.dumps(payload), headers=headers)
      print("{} was added".format(nid))
  
  while True:
  
  #Trigger User Input
      new_item = input("&gt; ")
  
  #Add nid
      if new_item == "A":
          nid = input("Give me an NID please: ")
          payload(nid)
          add(nid)
  </code></pre>