avorter
avorter

Reputation: 119

python GET and POST with json and variables

get parse post

I can't seem to get this to work. This is what I think is happening: (But obviously I am doing something wrong.)

  1. User gives me in the form of nid.
  2. Take nid and construct url to GET json dump.
  3. Take value of title and body from json dump and store in variables title and body.
  4. Construct another url with the various variables and POST to site 2.

Attempt:

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("> ")

#Add nid
    if new_item == "A":
        nid = input("Give me an NID please: ")
        payload(nid)
        add(nid)

Upvotes: 0

Views: 4766

Answers (1)

Tanu
Tanu

Reputation: 1563

You have not passed title ,body in the def add(nid) function parameters/arguments. So to correct this, modify def payload(nid) to return body/title & pass this arguments as to add(nid,title,body) function.

Hope this helps!

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"])
    return (title,body)
#Take title, body, auth_token and then also nid from user input, and POSTing  to site 2 
def add(nid,title,body):
    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("> ")

#Add nid
    if new_item == "A":
        nid = input("Give me an NID please: ")
        title,body = payload(nid)
        add(nid,title,body)

Upvotes: 1

Related Questions