URLSearchParams request bodies cannot be read (2023)

URLSearchParams request bodies cannot be read (1)

Lightrun Answers is designed to reduce the constant Google searching that comes with debugging third-party libraries. It collects links to all the places you might want to check while chasing a tough bug.

And if you're still stuck on the end, we'd love to give you a call to see how we can help.

error description

requirements

environment check

  • i use thoseMost recent mwexecution
  • I'm using Node.js version 14 or higher

Node.js version

v16.13.0

breeding stock

https://github.com/markdon/cra-msw

reproductive stages

Repo Klonenasl andDEBUG=* npm run test -- --watchAll=false

current behavior

I think it has to dohttps://github.com/mswjs/msw/issues/1318

When processing a search request where the request body is a URLSearchParameters object, the body in the handler cannot be read. This was previously available viaOrder.Body(v0.27.1).Order.Bodyit is now obsolete.

I would expect to be able to use only the newestrequest.text(), but this seems to throw an error when parsing the body.

debug logs. See ERR_INVALID_ARG_TYPE
➜ cra-msw git:(main) DEBUG=* npm run test -- --watchAll=false

cra-msw@0.1.0 testreact-scripts test „–watchAll=false“

babel:config:loading:files:plugins Loaded preset '/Users/mark/projects/cra-msw/node_modules/babel-preset-react-app/index.js' from '/Users/mark/projects/cra-msw' . +0msbabel:config:loading:files:plugins Loaded preset "/Users/mark/projects/cra-msw/node_modules/babel-preset-jest/index.js" from "/Users/mark/projects/cra-msw". +2mshttp creates the interceptor... +0msxhr creates the interceptor... +0mssetup-server creates the interceptor... +0mshttp:adds to the "request" event listener: setupServerListener +0msasync-event-emitter:adds to the "request" " listener... + 0msxhr:on add request listener: setupServerListener +0msasync-event-emitter:on add request listener… +0mshttp:on add response event listener: +0msasync-event-emitter:on " add response" listener.. . ACTIVE +0mssetup server: apply activates the issuer! ACTIVE +0mssetup-server got global instance: undefined +4mssetup-server:apply found no instance running, setting up new instance... +1mssetup-server:setup applies all 2 interceptors... +0mssetup-server:setup applies " ClientRequestInterceptor "- Interceptor on... +0mshttp:apply apply interceptor... +0msasync-event-emitter:activate state set to: ACTIVE +0mshttp:apply activate emitter! ACTIVE +0mshttp retrieved global instance: undefined +4mshttp:apply found no running instance, configure new instance... +0mshttp:configure native "http" module fixed! +0mshttp:setup native "https" module fixed! +0mshttp set global instance! http +0mssetup-server:setup Add interceptor Remove signature +1mssetup-server:setup Apply XMLHttpRequestInterceptor interceptor... +0msxhr:apply Apply interceptor... +0msasync-event-emitter:enable Set status to: ACTIVE +0msxhr:apply active the issuer! ACTIVE +0msxhr Global instance fetched: undefined +5msxhr:apply did not find any running instance, configure new instance... +0msxhr:XMLHttpRequest module patch configuration... +0msxhr:XMLHttpRequest module configuration native fixed! Set global instance XMLHttpRequestOverride +0msxhr! xhr +0mssetup-server:setup Add Interceptor Discard signature +0mssetup-server Set global instance! setup-server +1msxhr: prompt for POST /open login {method: 'POST', url: '/login', async: true, user: undefined, password: undefined} +0msxhr: prompt for POST /reset login +1msxhr: prompt for POST /login readyState change 0 -> 1 +1msxhr: POST request /login triggers readystate change... +0msxhr: POST request /login trigger "readystatechange" (1) +0msxhr: POST request /login resolve listener for "readystatechange" + 0msxhr : POST request /login Set the content-type request header to "application/x-www-form-urlencoded;charset=UTF-8" +0msxhr: POST request /login POST /login request +0msxhr: POST /login request Submit request header HeadersPolyfill { [Symbol(normalizedHeaders)]: { 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8' },[Symbol(rawHeaderNames)]: Map(1) { 'content -type' => 'content-type' }} +1msxhr:request POST /login emitting 'request' event to 1 listener... +0msasync-event-emitter:emit emitting 'request' event ... + 0msasync-event-emitter:openListenerQueue opens listen queue "Request" ... +0msasync-event-emitter:openListenerQueue no queue found, create one ... +0msasync-event-emitter:Emit and attach a one-time cleanup "Request" -Listeners... +0msasync-event-emitter:openListenerQueue Opening listener queue "Request"... +0msasync-event-emitter:openListenerQueue returns an existing queue: [] +0msasync-event-emitter: on is waiting for "Request"-Listener... +11msxhr:request POST /login waiting for mock response... +2msasync-event-emitter:on " TypeError [ERR_INVALID_ARG_TYPE]: Argument "input" must be an instance of ArrayBuffer or ArrayBufferView. Got an instance of listener URLSearchParams" rejected! +13msxhr:request POST /login middleware function threw exception! TypeError: Argument "input" must be an instance of ArrayBuffer or ArrayBufferView. An instance of URLSearchParamsat new NodeError (node:internal/errors :371:5)in TextDecoder.decode (node:internal/encoding:413:15)in Object.decodeBuffer (/Users/mark/projects/cra-msw/node_modules/@mswjs/interceptors/src/utils/bufferUtils.ts:11:18) in RestRequest.(/Users/mark/projects/cra-msw/node_modules/@mswjs/interceptors/src/IsomorphicRequest.ts:59:12)bei Schritt (/Users/mark/projects/cra-msw/node_modules/@mswjs/interceptors/lib/IsomorphicRequest.js:33:23) bei Object.next (/Users/mark/projects/cra-msw/node_modules/@mswjs/interceptors/lib/IsomorphicRequest.js:14:53) até /Users/mark/projects/cra-msw/node_modules/@mswjs/interceptors/lib/IsomorphicRequest.js:8:71in the new promise () not object..__awaiter (/Users/mark/projects/cra-msw/node_modules/@mswjs/interceptors/lib/IsomorphicRequest.js:4:12) e RestRequest.Object..IsomorphicRequest.text (/Users/mark/projects/cra-msw/node_modules/@mswjs/interceptors/lib/IsomorphicRequest.js:73:16) {Code: 'ERR_INVALID_ARG_TYPE'} +12msxhr: POST request /login trigger "error" (1) +8msxhr: POST request /login resolve event "error" listener +1msxhr:request POST /login abort +0msxhr: POST /login request ready state change 1 -> 0 +0msxhr: POST /login request trigger "abort" (0) +0msxhr: POST /login request resolve listener for event "abort" +0msasync-event- emitter :emit cleared the queue of "Request" listeners! +27mssetup-server:dispose Dispose of interceptors... +0mssetup-server retrieved global instance: BatchInterceptor +49mssetup-server global instance deleted! configuration server +0mssetup-server:dispose global token deleted: undefined +0mssetup-server:dispose 2 dispose signatures... +0mshttp:dispose interceptor... +0mshttp global instance retrieved: ClientRequestInterceptor +51mshttp global instance deleted! http +0mshttp:dispose global token deleted: undefined +0mshttp:dispose Disposing 2 signatures... +0mshttp:native "http" module configuration restored! +51mshttp:Restored native configuration "https" module! +0mshttp:delete all discarded signatures! 0 +0msasync-event-emitter:Disable removing all listeners... +0msasync-event-emitter:removeAllListeners event: undefined +0msasync-event-emitter:removeAllListeners has deleted the listener queue! Map(0) {} +0msasync-event-emitter:deactivate Set status to: DISABLED +0mshttp:dispose destroyed listener! +0msxhr:dispose discard interceptors... +0msxhr retrieved global instance: XMLHttpRequestInterceptor +50msxhr deleted global instance! xhr +0msxhr:dispose global symbol deleted: undefined +0msxhr:dispose 1 Dispose of signatures... +0msxhr:native XMLHttpRequest module restored! XMLHttpRequest +51msxhr:dispose discards all signatures! 0 +1msasync-event-emitter: Disable removing all listeners... +0msasync-event-emitter:removeAllListeners event: undefined +0msasync-event-emitter:removeAllListeners deleted listener queue! Map(0) {} +0msasync-event-emitter:deactivate Set status to: DISABLED +0msxhr:dispose destroyed the listener! +0mssetup-server:discard all discarded signatures! 0 +2msasync-event-emitter: Disable removing all listeners... +0msasync-event-emitter:removeAllListeners event: undefined +0msasync-event-emitter:removeAllListeners deleted queue of listeners! Map(0) {} +0msasync-event-emitter:deactivate Set status to: DISABLED +0mssetup-server:dispose destroyed the listener! +0msFAIL src/Login.test.tsfetch✕ can login (35ms)

● fetch › can register

Expect(received).resolves.toBeInstanceOf()Received Promise abgelehnt statt aufgelöstRejected to value: [TypeError: Network request failed] 2 | description('fetch', ()=>{ 3 | it('can login', async ()=>{> 4 | await wait(fetch('/login', { | ^ 5 | método: 'POST', 6 | body: new URLSearchParams('username=admin&password=admin') 7 | })).resolves.toBeInstanceOf(Response); at Expect (node_modules/expect/build/index.js:178:15) at Object.<anonymous > (src/Login.test.ts:4:11) bei TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13) bei runJest (node_modules/@jest/core/build/runJest. js:404:19) bei _run10000 (node_modules/@jest/core/build/cli/index.js:320:7) bei runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

Test sets: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 0.834 s, estimated 1 sRan all test sets.

Expected behavior

A request text of type URLSearchParameters must be readable in a handler. I suggest morerequest.text(), which would return the value of URLSearchParameters.toString() .

URLSearchParams request bodies cannot be read (2)GitHub's best comments

2reactions

sweet boycommented on September 9, 2022

Same problem as mine#1327but withform dataBody.mwjust broke read request bodies in v0.44.0.

New features are being added all the time in the 0.x line, so it's perfectly fine to break things from Semver's perspective. And mistakes are not corrected.

#Sadness

1Reaction

ianldgscommented on September 2, 2022

My workaround, which was really forform data, The following is:

jest.spyOn(global, "FormData").mockImplementation(mockFormDataForMSW);jest.mock("util", mockNodeUtilForMSW);function mockFormDataForMSW() { return new FormDataFakeArrayBuffer(0) tão desconhecido quanto FormData;}function mockNodeUtilForMSW() { const real = jest.requireActual("util") as typeof import("util"); return { ...real, TextDecoder: classe estendida real.TextDecoder { decode( input?: NodeJS.ArrayBufferView | ArrayBuffer | nulo, opções?: { stream?: booleano | indefinido }, ): string { if (instância de entrada de FormDataFakeArrayBuffer) return input.toString(); super.decode (Eingabe, Optionen) zurückgeben; } }, };}class FormDataFakeArrayBuffer erweitert ArrayBuffer { private params = new URLSearchParams(); append(param: string, value: string) { return this.params.append(param, value); } toString() { return this.params.toString(); }}

For reference/SEO, the error was:

The input argument must be an instance of ArrayBuffer or ArrayBufferView. Get an instance of FormData

or

The input argument must be an instance of ArrayBuffer or ArrayBufferView. Get an instance of URLSearchParams
Read more feedback on GitHub >

URLSearchParams request bodies cannot be read (3)Top results from across the web

value} cannot be assigned to an undefined argument type {property

The problem is that refresh_token might not be set and URLSearchParams only supports string-valued fields.

Simple URL manipulation with URLSearchParams

Chrome 49 implements the URL spec's URLSearchParams, a useful API for playing with URL query parameters.

URLSearchParams.toString() - Web-APIs | MDN

The toString() method of the URLSearchParams interface returns a query string suitable for use in a URL.

Anfrage · Cloudflare Workers Docs

The request interface represents an HTTP request and is part of the ... body string | ReadableStream | form data | URLSearchParams optional.

Request Item - Akamai TechDocs

Properties Body A stream used to read the contents of the request body. This is a... Header variable must not be defined

...

Read more >

URLSearchParams request bodies cannot be read (4)Most important related medium post

no results found

URLSearchParams request bodies cannot be read (5)Top questions related to StackOverflow

no results found

URLSearchParams request bodies cannot be read (6)Live code troubleshooting

Lightrun allows developers to add logs, metrics and snapshots to live code, without the need for reboots or redeployments.

start for free

URLSearchParams request bodies cannot be read (7)Top Reddit related topics

no results found

URLSearchParams request bodies cannot be read (8)Top posts related to Hackernoon

no results found

URLSearchParams request bodies cannot be read (9)Related top tweets

no results found

URLSearchParams request bodies cannot be read (10)Top developer related posts

no results found

URLSearchParams request bodies cannot be read (11)Top posts related to Hashnode

no results found

References

Top Articles
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated: 14/11/2023

Views: 6250

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.