Get and Post request to custom fields in word press rest api [python]

Question

Im trying to send a get/post request to my word press website using the following code. However, iam neither able to GET/view custom fields, nor POST request custom fields. The Get and post request go through the Rest API and to the website, however it returns non of the custom fields. Title and taxonomy fields(drop down selections) are obtain through get and post request but the custom fields are not.

I tried using 3rd party plugins, which didn’t help.

In the below code, I’am initiating a post request, where by var1 is a taxonomy(drop-down) field and var2 is a custom field. The code doesn’t return the var2 value and is completely ignored.

Is there a method to allow get/post interaction with custom fields?Have you faced similar problem? Whats the root cause and solution?

import base64
import requests
import json

url="https://www.mywebsite.com/wp-json/wp/v2/cars"
user="myusername"
password='mypassword'
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8'),'Content-Type': 'application/json','User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url , headers=header)
x='{"title":"Welcome","var1":[2478],"var2":"50"}'
x=json.loads(x)
r=requests.post(url+'?status=publish',headers=header,json=x)
print(r.text)

Code Output:
{"title":"Welcome","var1":[2478]}

0
The Oracle 3 weeks 2022-01-06T14:10:54-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse