Skip to content

Axios Authentication WP-API #73

Description

@techhjork

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions