WordPress/gutenberg

apiFetch should be able to handle a 200 response code with an empty body

Open

#21.899 aperta il 26 apr 2020

Vedi su GitHub
 (13 commenti) (0 reazioni) (1 assegnatario)JavaScript (3893 fork)batch import
Good First IssueNeeds DevREST API Interaction[Package] API fetch[Status] In Progress[Type] Enhancement

Metriche repository

Star
 (9607 star)
Metriche merge PR
 (Merge medio 12g 18h) (509 PR mergiate in 30 g)

Descrizione

Describe the bug If the response is 200 but the body is empty, it causes an error: image

The code handles 204 responses, but not 200 with empty body: https://github.com/WordPress/gutenberg/blob/%40wordpress/api-fetch%403.13.1/packages/api-fetch/src/utils/response.js#L16-L20

From https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204

The common use case is to return 204 as a result of a PUT request, updating a resource, without changing the current content of the page displayed to the user. If the resource is created, 201 Created is returned instead. If the page should be changed to the newly updated page, the 200 should be used instead.

So, since a 200 with empty body isn't a wrong response, the apiFetch should handle it instead of throwing a try/catch error.

https://mcculloughwebservices.com/2016/09/23/handling-a-null-response-from-an-api/ might be helpful.

Guida contributor