Home > other >  How to create variable about this wordpress api made on python
How to create variable about this wordpress api made on python

Time:12-26

import requests
import json
import base64

url = "http://localhost/wordpress/wp-json/wp/v2"
user = "test1"
password = "kATN 3ahO Q6Ps pBdZ g0Ko C5zy"


creds = user   ":"   password
token = base64.b64encode(creds.encode())

header = {"Authorization": 'Basic '   token.decode('utf-8')}

post =    {
    "title": "Posts", # // How to convert this on varible?
    "singular_name": "Post", 
    "content": "this is a content", # // How to convert this on varible?
    "status": "publish"
}
r = requests.post(url   '/posts',headers=header, json=post)
print(r)

Hi, guys im very newbee i wanted to create a varible in this line. i want to create variable on title and content.. idk how to do it

CodePudding user response:

You already have variables in your code. For example, header is a variable. Here's how your code could look like with what you asked about:

import requests
import json
import base64

url = "http://localhost/wordpress/wp-json/wp/v2"
user = "test1"
password = "kATN 3ahO Q6Ps pBdZ g0Ko C5zy"


creds = user   ":"   password
token = base64.b64encode(creds.encode())

header = {"Authorization": 'Basic '   token.decode('utf-8')}

your_title = "Title"
your_content = "Content"

post =    {
    "title": your_title,
    "singular_name": "Post", 
    "content": your_content,
    "status": "publish"
}
r = requests.post(url   '/posts',headers=header, json=post)
print(r)
  • Related