sindresorhus/got

`Response<string>` returned rather than a json response

gitgrimbo opened this issue · 2 comments

Describe the bug

  • Node.js version: v20.8.1
  • OS & version: Windows 10

Actual behavior

Response<string> returned rather than a json response.

image

image

...

Expected behavior

Should return a response with a json() method, like in the repo readme example.

Code to reproduce

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "exports": "./index.js",
  "type": "module",
  "scripts": {
    "start": "node --loader ts-node/esm ./src/index.ts"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/got": "^9.6.12",
    "ts-loader": "^9.5.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.3.2"
  },
  "dependencies": {
    "got": "^14.0.0"
  }
}

index.ts

import got from "got";

async function main() {
  const response = await got.post("https://httpbin.org/anything", {
    json: {
      hello: "world",
    },
  });
  console.log(response);
  console.log(response.json());
}

main();

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

https://www.npmjs.com/package/got-cjs
import got from "got-cjs";

@gitgrimbo you should specify responseType: "json" in options, see the docs.