WP-API / Basic-Auth

Basic Authentication handler for the JSON API, used for development and debugging purposes
761 stars 281 forks source link

Axios Authentication WP-API #73

Open techhjork opened 2 years ago

techhjork commented 2 years ago

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)