How to read and debug JSON API responses

The checkout page shows “৳NaN”, the app crashes with “Unexpected token <”, or a list is simply empty. Nine times out of ten the bug is not in your code but in a wrong assumption about what the API actually sent. The fastest way to debug is to stop guessing and look at the raw response: its status code, its headers and its body. This guide shows how to do that with the browser, curl and Postman, how to point at exactly the value you need, and which API habits cause the most trouble.

1. Inspect it in the browser’s DevTools

For anything your web page requests, the browser already has the full answer.

  1. Press F12 (or Ctrl+Shift+I) and open the Network tab.
  2. Click the Fetch/XHR filter so you only see API calls, then reload the page or repeat the action.
  3. Click a request. Headers shows the URL, method, status code and Content-Type; Preview shows the JSON as a collapsible tree; Response shows the raw text exactly as received.

Two features save a lot of time. Preserve log keeps requests when the page redirects. And right-clicking a request gives Copy → Copy as cURL, which reproduces the exact request — headers, cookies and body — in a terminal.

Careful when sharing: a copied cURL command contains your cookies and Authorization token. Remove them before pasting it into a chat or a bug report.

2. Use curl in the terminal

curl ships with Linux, macOS and current Windows 10/11. The flags you need most:

# -i: show the status line and response headers above the body
curl -i https://api.example.com/v1/products?page=1

# -s: silent (no progress bar), so the output can be piped to a formatter
curl -s https://api.example.com/v1/products | python -m json.tool

# with jq you can also pick out one value
curl -s https://api.example.com/v1/products | jq '.data.items[0].price'

# send JSON and a token
curl -s -X POST https://api.example.com/v1/orders \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  --data @order.json

On Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest, so type curl.exe to get the real curl. Putting the request body in a file with --data @order.json also avoids painful quote escaping in Windows shells.

3. Use Postman (or a similar client)

Postman, Insomnia, Bruno and VS Code REST extensions are handy when you call the same endpoints repeatedly. They show the status, time and size next to the body; the Pretty view formats JSON, and the headers are in a separate tab. Save requests in a collection, keep tokens in environment variables rather than in the URL, and paste a cURL command from DevTools to import a request in one step.

4. Read the status and Content-Type first

Before you look at the body, check these two things. They tell you whether the body is what you expect at all.

StatusMeaningWhat the body usually is
200, 201Success / createdThe data you asked for
204Success, no contentEmpty — calling JSON.parse("") throws
400, 422Your input was rejectedA JSON error object; read it, it usually names the field
401, 403Not logged in / not allowedJSON error, or an HTML login page
404Wrong URL or missing recordOften an HTML page from the web server
429Too many requestsCheck the Retry-After header
500, 502, 503, 504Server or gateway failureFrequently HTML from a proxy or load balancer

The Content-Type header should be application/json (possibly with ; charset=utf-8). If it is text/html, parsing will fail no matter what. A defensive fetch helper checks both before parsing:

async function getJson(url, options = {}) {
  const res = await fetch(url, { ...options, headers: { Accept: 'application/json', ...options.headers } });
  const type = res.headers.get('content-type') || '';
  const text = await res.text();
  if (!type.includes('application/json')) {
    throw new Error(`Expected JSON but got ${res.status} ${type}: ${text.slice(0, 120)}`);
  }
  const body = text ? JSON.parse(text) : null;
  if (!res.ok) {
    throw new Error(`API error ${res.status}: ${JSON.stringify(body)}`);
  }
  return body;
}

Reading the body as text first means that when something goes wrong, your error message contains the start of what the server actually sent — usually enough to see the problem immediately.

5. Point at the exact value with a JSON path

Large responses are easier to talk about with a path. The common notation starts with $ for the root, uses dots for keys and brackets for array positions (starting at 0):

{
  "data": {
    "items": [
      { "id": 501, "name": "Rice 5kg", "price": 420 },
      { "id": 502, "name": "Lentils 1kg", "price": 145 }
    ]
  }
}

$.data.items[0].price      → 420
$.data.items[1].name       → "Lentils 1kg"

The same path works almost unchanged in code: body.data.items[0].price in JavaScript, body["data"]["items"][0]["price"] in Python, or .data.items[0].price in jq. Keys with spaces or hyphens need brackets: $["order-id"]. In JavaScript, optional chaining (body.data?.items?.[0]?.price) avoids a crash when part of the path is missing.

Tip: in the formatter’s Tree view, click any key and the path bar shows its full path, ready to copy into a bug report or your code.

6. Recognise envelopes and pagination

Few APIs return a bare list. Most wrap the data in an envelope with metadata, and split long lists into pages. Two common shapes:

// Page numbers
{ "data": [ ... ], "meta": { "page": 2, "per_page": 20, "total": 134 } }

// Cursor
{ "data": [ ... ], "next_cursor": "eyJpZCI6NTIwfQ" }

With cursors you keep requesting until the cursor is empty:

async function fetchAll(baseUrl) {
  const all = [];
  let cursor = null;
  do {
    const url = cursor ? `${baseUrl}?cursor=${encodeURIComponent(cursor)}` : baseUrl;
    const page = await getJson(url);
    all.push(...page.data);
    cursor = page.next_cursor;
  } while (cursor);
  return all;
}

If a list looks “randomly” short, check for meta.total or a next link — you are probably looking at page one.

7. Common API mistakes to watch for

Make a messy response readable. Paste the raw body into the formatter, switch to Tree view and click any value to copy its path. Try it now

FAQ

Why do I get a CORS error when the same URL works in curl?

CORS is enforced only by browsers. The server must send headers such as Access-Control-Allow-Origin for your site. curl and Postman ignore CORS, so the fix belongs on the server (or a backend proxy), not in your fetch call.

Preview shows data but my code gets undefined. Why?

Usually the path is one level off — the data is inside an envelope such as body.data, or you forgot to await res.json(). Copy the exact path from a tree view and compare.

How do I handle IDs larger than 253 in JavaScript?

Best: have the API send them as strings. If you can’t, newer runtimes (including current Node.js and Chrome) pass the original text to a JSON.parse reviver as context.source, which you can turn into a BigInt; check browser support before relying on it.

API-এর JSON রেসপন্স কীভাবে পড়বেন ও ডিবাগ করবেন

চেকআউট পেজে দেখাচ্ছে “৳NaN”, অ্যাপ ক্র্যাশ করছে “Unexpected token <” বলে, কিংবা লিস্টটা স্রেফ ফাঁকা। দশবারের মধ্যে নয়বারই বাগটা আপনার কোডে নয় — API আসলে কী পাঠিয়েছে, সে সম্পর্কে একটা ভুল ধারণায়। ডিবাগ করার সবচেয়ে দ্রুত উপায় হলো অনুমান বাদ দিয়ে মূল রেসপন্সটা দেখা: স্ট্যাটাস কোড, হেডার আর বডি। এই গাইডে দেখানো হয়েছে ব্রাউজার, curl আর Postman দিয়ে কীভাবে তা দেখবেন, দরকারি মানটি কীভাবে নির্দিষ্ট করে ধরবেন, আর API-এর কোন অভ্যাসগুলো সবচেয়ে বেশি ঝামেলা বাধায়।

১. ব্রাউজারের DevTools-এ দেখুন

আপনার ওয়েবপেজ যা রিকোয়েস্ট করে, তার পুরো উত্তর ব্রাউজারের কাছেই আছে।

  1. F12 (বা Ctrl+Shift+I) চেপে Network ট্যাব খুলুন।
  2. Fetch/XHR ফিল্টারে ক্লিক করুন, যাতে শুধু API কলগুলো দেখা যায়; তারপর পেজ রিলোড করুন বা কাজটা আবার করুন।
  3. একটি রিকোয়েস্টে ক্লিক করুন। Headers-এ থাকে URL, মেথড, স্ট্যাটাস কোড আর Content-Type; Preview-এ JSON দেখা যায় ভাঁজ-খোলা যায় এমন ট্রি আকারে; Response-এ ঠিক যেমন এসেছে তেমন কাঁচা লেখা।

দুটি ফিচার অনেক সময় বাঁচায়। Preserve log চালু রাখলে পেজ রিডাইরেক্ট হলেও রিকোয়েস্টগুলো মুছে যায় না। আর রিকোয়েস্টে রাইট-ক্লিক করে Copy → Copy as cURL দিলে হুবহু একই রিকোয়েস্ট — হেডার, কুকি, বডিসহ — টার্মিনালে আবার চালানো যায়।

শেয়ার করার সময় সাবধান: কপি করা cURL কমান্ডে আপনার কুকি আর Authorization টোকেন থাকে। চ্যাটে বা বাগ রিপোর্টে পেস্ট করার আগে সেগুলো মুছে দিন।

২. টার্মিনালে curl ব্যবহার করুন

Linux, macOS আর বর্তমান Windows 10/11-এ curl আগে থেকেই থাকে। সবচেয়ে বেশি লাগে এই ফ্ল্যাগগুলো:

# -i: বডির ওপরে স্ট্যাটাস লাইন আর রেসপন্স হেডার দেখায়
curl -i https://api.example.com/v1/products?page=1

# -s: silent (প্রগ্রেস বার নেই), যাতে আউটপুট ফরম্যাটারে পাইপ করা যায়
curl -s https://api.example.com/v1/products | python -m json.tool

# jq থাকলে একটি নির্দিষ্ট মানও বের করা যায়
curl -s https://api.example.com/v1/products | jq '.data.items[0].price'

# JSON আর টোকেন পাঠানো
curl -s -X POST https://api.example.com/v1/orders \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  --data @order.json

Windows PowerShell 5.1-এ curl আসলে Invoke-WebRequest-এর alias, তাই আসল curl পেতে লিখুন curl.exe। রিকোয়েস্ট বডি একটি ফাইলে রেখে --data @order.json দিলে Windows শেলে কোট এসকেপ করার যন্ত্রণাও এড়ানো যায়।

৩. Postman (বা এ ধরনের ক্লায়েন্ট) ব্যবহার করুন

একই এন্ডপয়েন্ট বারবার কল করতে হলে Postman, Insomnia, Bruno বা VS Code-এর REST এক্সটেনশন কাজে আসে। এগুলো বডির পাশে স্ট্যাটাস, সময় আর আকার দেখায়; Pretty ভিউ JSON সাজিয়ে দেয়, আর হেডার থাকে আলাদা ট্যাবে। রিকোয়েস্টগুলো একটি collection-এ সেভ করুন, টোকেন URL-এ না রেখে environment variable-এ রাখুন, আর DevTools থেকে কপি করা cURL কমান্ড পেস্ট করে এক ধাপেই রিকোয়েস্ট ইমপোর্ট করুন।

৪. আগে স্ট্যাটাস আর Content-Type দেখুন

বডি দেখার আগে এই দুটি জিনিস যাচাই করুন। এগুলোই বলে দেয় বডিটা আদৌ আপনার প্রত্যাশামতো কি না।

স্ট্যাটাসমানেবডিতে সাধারণত কী থাকে
200, 201সফল / তৈরি হয়েছেযে ডেটা চেয়েছিলেন
204সফল, কোনো কনটেন্ট নেইফাঁকা — JSON.parse("") এরর দেয়
400, 422আপনার ইনপুট বাতিল হয়েছেএকটি JSON এরর অবজেক্ট; পড়ুন, সাধারণত কোন ফিল্ডে সমস্যা তা বলা থাকে
401, 403লগইন নেই / অনুমতি নেইJSON এরর, অথবা HTML লগইন পেজ
404ভুল URL বা রেকর্ড নেইপ্রায়ই ওয়েব সার্ভারের একটি HTML পেজ
429অনেক বেশি রিকোয়েস্টRetry-After হেডার দেখুন
500, 502, 503, 504সার্ভার বা গেটওয়ের সমস্যাপ্রায়ই প্রক্সি বা লোড ব্যালান্সারের HTML

Content-Type হেডার হওয়া উচিত application/json (সাথে ; charset=utf-8 থাকতে পারে)। যদি text/html হয়, তাহলে যা-ই করুন পার্সিং ব্যর্থ হবে। একটি সতর্ক fetch হেল্পার পার্স করার আগে দুটোই যাচাই করে:

async function getJson(url, options = {}) {
  const res = await fetch(url, { ...options, headers: { Accept: 'application/json', ...options.headers } });
  const type = res.headers.get('content-type') || '';
  const text = await res.text();
  if (!type.includes('application/json')) {
    throw new Error(`Expected JSON but got ${res.status} ${type}: ${text.slice(0, 120)}`);
  }
  const body = text ? JSON.parse(text) : null;
  if (!res.ok) {
    throw new Error(`API error ${res.status}: ${JSON.stringify(body)}`);
  }
  return body;
}

বডি আগে টেক্সট হিসেবে পড়ার সুবিধা হলো, কিছু ভুল হলে আপনার এরর মেসেজেই থাকবে সার্ভার আসলে কী পাঠিয়েছিল তার শুরুটা — সমস্যা চিনতে সাধারণত সেটুকুই যথেষ্ট।

৫. JSON পাথ দিয়ে ঠিক মানটি নির্দেশ করুন

বড় রেসপন্স নিয়ে কথা বলা সহজ হয় পাথ দিয়ে। প্রচলিত নিয়মে রুটের জন্য $, কী-এর জন্য ডট আর অ্যারের অবস্থানের জন্য ব্র্যাকেট (গোনা শুরু 0 থেকে):

{
  "data": {
    "items": [
      { "id": 501, "name": "Rice 5kg", "price": 420 },
      { "id": 502, "name": "Lentils 1kg", "price": 145 }
    ]
  }
}

$.data.items[0].price      → 420
$.data.items[1].name       → "Lentils 1kg"

একই পাথ কোডেও প্রায় হুবহু চলে: JavaScript-এ body.data.items[0].price, Python-এ body["data"]["items"][0]["price"], jq-তে .data.items[0].price। কী-তে স্পেস বা হাইফেন থাকলে ব্র্যাকেট লাগবে: $["order-id"]। JavaScript-এ optional chaining (body.data?.items?.[0]?.price) দিলে পাথের কোনো অংশ না থাকলেও ক্র্যাশ হয় না।

টিপ: ফরম্যাটারের ট্রি ভিউ-তে যেকোনো কী-তে ক্লিক করলে পাথ বারে তার পুরো পাথ দেখায়, যা বাগ রিপোর্ট বা কোডে কপি করার জন্য তৈরি।

৬. এনভেলপ আর পেজিনেশন চিনে নিন

খুব কম API শুধু একটা খালি লিস্ট ফেরত দেয়। বেশিরভাগই ডেটাকে মেটাডেটাসহ একটি এনভেলপে মুড়ে দেয়, আর লম্বা লিস্ট ভাগ করে পেজে পেজে পাঠায়। দুটি প্রচলিত রূপ:

// Page numbers
{ "data": [ ... ], "meta": { "page": 2, "per_page": 20, "total": 134 } }

// Cursor
{ "data": [ ... ], "next_cursor": "eyJpZCI6NTIwfQ" }

কার্সরের ক্ষেত্রে কার্সর ফাঁকা না হওয়া পর্যন্ত রিকোয়েস্ট করতে থাকুন:

async function fetchAll(baseUrl) {
  const all = [];
  let cursor = null;
  do {
    const url = cursor ? `${baseUrl}?cursor=${encodeURIComponent(cursor)}` : baseUrl;
    const page = await getJson(url);
    all.push(...page.data);
    cursor = page.next_cursor;
  } while (cursor);
  return all;
}

লিস্ট যদি “কোনো কারণ ছাড়াই” ছোট মনে হয়, meta.total বা একটি next লিংক আছে কি না দেখুন — সম্ভবত আপনি শুধু প্রথম পেজটা দেখছেন।

৭. API-এর যেসব সাধারণ ভুলে নজর রাখবেন

এলোমেলো রেসপন্স পড়ার উপযোগী করুন। কাঁচা বডি ফরম্যাটারে পেস্ট করুন, ট্রি ভিউতে যান আর যেকোনো মানে ক্লিক করে তার পাথ কপি করুন। এখনই চেষ্টা করুন

প্রশ্নোত্তর

একই URL curl-এ চলে, কিন্তু ব্রাউজারে CORS এরর দেয় কেন?

CORS শুধু ব্রাউজারই প্রয়োগ করে। আপনার সাইটের জন্য সার্ভারকে Access-Control-Allow-Origin-এর মতো হেডার পাঠাতে হবে। curl আর Postman CORS মানে না, তাই সমাধান সার্ভারে (বা একটি ব্যাকএন্ড প্রক্সিতে), আপনার fetch কলে নয়।

Preview-তে ডেটা দেখায়, কিন্তু কোডে পাই undefined। কেন?

সাধারণত পাথ এক ধাপ এদিক-ওদিক — ডেটা আছে body.data-এর মতো কোনো এনভেলপের ভেতরে, অথবা await res.json() করতে ভুলে গেছেন। ট্রি ভিউ থেকে সঠিক পাথ কপি করে মিলিয়ে দেখুন।

JavaScript-এ 253-এর চেয়ে বড় আইডি কীভাবে সামলাব?

সবচেয়ে ভালো: API যেন এগুলো স্ট্রিং হিসেবে পাঠায়। তা সম্ভব না হলে, নতুন রানটাইমগুলো (বর্তমান Node.js ও Chrome-সহ) JSON.parse-এর reviver-কে মূল লেখাটি context.source হিসেবে দেয়, যা থেকে BigInt বানানো যায়; তবে ভরসা করার আগে ব্রাউজার সাপোর্ট দেখে নিন।

Related guides

More free tools