Basic-Auth icon indicating copy to clipboard operation
Basic-Auth copied to clipboard

Axios Authentication WP-API

Open techhjork opened this issue 2 years ago • 0 comments

Trying WP API by Nodejs by Axios but getting Authentication issue

curl -i  -X POST \ -H --user admin:pass -H "Content-Type: application/json" -d '{"title":"POST TWO","content":"Two","status":"publish"}' http://www.test.localhost/?rest_route=/wp/v2/projects

Code in Image image

const express = require("express")
const axios = require("axios")
const app = express()
axios.defaults.baseURL = 'http://www.test.localhost/?rest_route=';
app.get("/",(req,res)=>{
	axios({
		method:"POST",
		headers:{
			'Content-Type': 'application/json;charset=utf8',
      		'Authorization':'Basic YWRtaW46cGFzcw=='
  		},
		url:'/wp/v2/projects',
		data:{"title":"POST three","content":"Two","status":"publish"},

	}).then(data=>{
		res.send(data)
	}).catch(err=>{
		console.log(err)
		res.send(err)
	})
})
app.listen(8000,()=>{
	console.log(`localhost:8000`)
})

working on CURL image

working on POSTMAN image

Getting this error

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'Socket'
    --- property '_httpMessage' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/home/tech/Desktop/Project/role/node_modules/express/lib/response.js:1128:12)
    at ServerResponse.json (/home/tech/Desktop/Project/role/node_modules/express/lib/response.js:260:14)
    at ServerResponse.send (/home/tech/Desktop/Project/role/node_modules/express/lib/response.js:158:21)
    at /home/tech/Desktop/Project/role/index.js:18:7
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

techhjork avatar Mar 13 '22 03:03 techhjork