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.
This PR changes the
PhylumApi.fetch
method to only set theContent-Type
header when it is sending content. Most API endpoints either expect a JSON request body withContent-Type: application/json
, or expect no content (and don't care aboutContent-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 theContent-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 usualPhylumApi.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
closes #<issueNum>
in description above)?