I´m trying to send a database (SQLite) as a JSON file and publish it in a Wordpress (Also i want to edited rows and column, etc) but when i do this doing a simple post with request library in python throws me this error when i use this line of code: header = {"user": username, "password": password} response = requests.post(url, headers=header, json=data) {"code":"rest_cannot_edit","message":"Lo siento, no tienes permisos para editar esta entrada.","data":{"status":401}} BUT, when i use auth inside this requests.post function, it works, it sends the information just using this: response = requests.post(url, auth=(username, password), json=data) Also my response.status is 200 (Post Correctly DONE!) According to all forums that i found, i need the Plugin Application Passwords, yes i already create my TOKEN password and just works like above, WHY? All code: import requests #PAGE WHERE I WANT TO SEND INFO url = "https://dca-mx.com/wp-json/wp/v2/pages/362" username = 'Fabian' # The application password you generated password = 'XXXX XXXX XXXX XXXX XXXX' # The post data data = { 'title': 'DCA DB', 'content': 'HELLO FROM PYTHON.', 'status': 'publish' } header = {"user": username, "password": password} # Send the HTTP request THIS WORKS response = requests.post(url, auth=(username, password), json=data) #THIS DOES NOT WORKS WHY??????!!!!!!!!!!! response = requests.post(url, headers=header, json=data) # Check the response if response.status_code == 201: print('Post created successfully') elif response.status_code == 200: print('Posteado Correctamente!') else: print(response.text)
Jese Leos
August 10, 2024
Verified user
The problem is about how authentication is being handled in your requests.post call. There are two primary methods to handle authentication with the WordPress REST API. When you are using the auth parameter, you are using Basic Authentication, which is why it works, check your code: response = requests.post(url, auth=(username, password), json=data). Altough the auth parameter internally sets the correct Authorization header for Basic Athentication, when using headers directly, you need to correctly format the Authorization header. The header dictionary you are using is not correctly formatted for Basic Authentication. So, you need to set the Authorization header with a base64-encoded string of username:password. Try with this: import requests import base64 # PAGE WHERE I WANT TO SEND INFO url = "https://dca-mx.com/wp-json/wp/v2/pages/362" username = 'Fabian' # The application password you generated password = 'XXXX XXXX XXXX XXXX XXXX' # The post data data = { 'title': 'DCA DB', 'content': 'HELLO FROM PYTHON.', 'status': 'publish' } # Encode the username and password credentials = f"{username}:{password}" encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8') header = {"Authorization": f"Basic {encoded_credentials}"} # Send the HTTP request (it should still work) response = requests.post(url, headers=header, json=data) # Now, check the response if response.status_code == 201: print('Post created successfully') elif response.status_code == 200: print('Posteado Correctamente!') else: print(response.text) Hope it helps, keep me updated!