Skip to content

workers-tinygo main.go /echo API: Body has already been used error #176

@nothize

Description

@nothize

Testing procedure:

  1. Create a new worker using the workers-tinygo template.
  2. Run the worker locally.
  3. Run curl -X POST -d "data" http://127.0.0.1:8787/echo .
  4. The below error will be seen:

TypeError: Body has already been used. It can only be used once. Use tee() first if you need to read it twice.
at async Object.fetch (file:///c:/project_folder/node_modules/miniflare/dist/src/workers/core/entry.worker.js:4323:22)

Expected result should be:

data

workers-go main.go /echo API has no such error, the code difference is as below:

workers-go:

		b, err := io.ReadAll(req.Body)
		if err != nil {
			panic(err)
		}
		io.Copy(w, bytes.NewReader(b))

workers-tinygo:

		io.Copy(w, req.Body)

Either apply the same change from workers-go or make use of below worked:

		r := io.TeeReader(req.Body, w)
		io.ReadAll(r)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions