How to Debug Node.js HTTP Requests

Know your error

Always read the fine print
Documentation directly contradicting itself

Don’t trust Web Developers

Error object 1
Error object 2 (exactly the same)

Postman

An example POST request with body
A successful POST response

Postman Echo

var newdata = {code: "************",client_id: "************",client_secret: "************",redirect_uri: "************"}fetch(`https://postman-echo.com/post`, {    method: 'POST',    body: JSON.stringify(newdata),    headers: {        'Content-Type': 'application/json; charset=utf-8',        'Access-Control-Allow-Origin': '*',    }})
.then(res => res.json())
.then(json => console.log(json))

Postman Echo didn’t return my data

Fetch

var data = {    code: "************",    client_id: "************",    client_secret: "************",    redirect_uri: "************"}fetch(`https://postman-echo.com/post`, {    method: 'POST',    body: JSON.stringify(data), // change to qs.stringify(data)    headers: {        'Content-Type': 'application/json; charset=utf-8',        "Access-Control-Allow-Origin": "*",    }}).then(res => res.json()).then(json => {    console.log(json) 
// your data is in json.data or json.form (see below)
})

Axios

var data = {    code: "************",    client_id: "************",    client_secret: "************",    redirect_uri: "************"}axios.post(`https://postman-echo.com/post`, data, {    headers: {        'Content-Type': 'application/json; charset=utf-8',        "Access-Control-Allow-Origin": "*",    }}).then((res) => {    console.log(res.data.data) // for urlencoded use res.data.form})

If you are using your own API

app.use(express.json())
app.use(express.urlencoded())

What a journey we have been through

--

--

--

Computer Scientist in London. I dabble in Node.js and I enjoy long walks on the beach

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Basics of Javascript · String · codePointAt() (method)

How to create a simple Bitcoin API with Node.js & GraphQL

7 Ridiculous NPM Packages You Won’t Believe Exist

To-Do list (Interview Scheduler) using Node.js , Express.js & MongoDB (Server Setup)

Developing JavaScript libraries and avoiding dependency hell

[LeetCode]LRU Cache — Generic Version

Stripe checkout with react and node — Part 2

Build a CRUD Service with Node.js, Express.js and MongoDB

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Harry Winer

Harry Winer

Computer Scientist in London. I dabble in Node.js and I enjoy long walks on the beach

More from Medium

Deploying an Express API to Google App Engine

What is Node.js?

Express vs Koa, Which Framework is Better?

What is Authorization (with examples)