phylum-dev / cli

Command line interface for the Phylum API
https://phylum.io
GNU General Public License v3.0
99 stars 10 forks source link

Only set content-type header with content #1390

Closed matt-phylum closed 3 months ago

matt-phylum commented 3 months ago

This PR changes the PhylumApi.fetch method to only set the Content-Type header when it is sending content. Most API endpoints either expect a JSON request body with Content-Type: application/json, or expect no content (and don't care about Content-Type at all). There are a couple API endpoints that optionally accept a request body which may or may not be JSON, and for those endpoints the Content-Type header must be set to JSON if and only if the request body is JSON. A null body is not JSON, so sending a request to one of those endpoints using the usual PhylumApi.fetch("v0", "/endpoint", { method: "POST" }) results in a 400 error when the server tries to decode a 0-byte body as JSON.

This PR doesn't attempt to do any sort of autodetection to determine if a request body is actually JSON. If there is a body and the type is not explicitly specified, it gets sent as JSON. This means if you want to send a text body you still need to set Content-Type: text/plain.

See this extension PR comment thread https://github.com/phylum-dev/community-extensions/pull/43#discussion_r1554221398

Checklist