Skip to content

TypeError: Can't read from request stream after response has been sent. when use stub.fetch(request) #14

@anusoft

Description

@anusoft

I want to read JSON body sent by HTTP POST (application/json) from Durable Object class, however I cannot read request.headers, request.text(), request.json() inside Durable object class, fetch function.

Refer from Durable Object Runtime APIs Document:

https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#sending-http-requests

let response = await stub.fetch(request);
let response = await stub.fetch(url, options);

and if you modify Counter example from

let resp = await obj.fetch(request.url);

to

let resp = await obj.fetch(request);

And send request to wrangler dev (wrangler 1.19.12) you'll got error

TypeError: Can't read from request stream after response has been sent. at line 0, col -2
{
  "exceptionDetails": {
    "columnNumber": -2,
    "exception": {
      "className": "TypeError",
      "description": "TypeError: Can't read from request stream after response has been sent.",
      "preview": {
        "description": "TypeError: Can't read from request stream after response has been sent.",
        "entries": null,
        "overflow": false,
        "properties": [
          {
            "name": "stack",
            "subtype": null,
            "type": "string",
            "value": "TypeError: Can't read from request stream after response has been sent.",
            "valuePreview": null
          },
          {
            "name": "message",
            "subtype": null,
            "type": "string",
            "value": "Can't read from request stream after response has been sent.",
            "valuePreview": null
          }
        ],
        "subtype": "error",
        "type": "object"
      },
      "subtype": "error",
      "type": "object",
      "value": null
    },
    "lineNumber": 0,
    "text": "Uncaught (async)",
    "url": "undefined"
  },
  "timestamp": 1655635954748
}

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