Skip to content

Don't send application\json content-type header with empty body on GET requests #19

Closed
@CalebM1987

Description

@CalebM1987

This library works great, but I have noticed issues with GET requests that have no request body on our backend API. The getFetchParams() always sends the Content-Type header as application\json, which throws the following error because the server expects a json body whenever that header is present:

"400 BadRequest: Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)"

We can manually override this by setting Content-Type to */* but it would be nice if it would check if there is body data before setting the content type header.

Suggested Fix:

function getFetchParams(request: Request) {
  const payload = { ...request.payload } // clone payload

  const path = getPath(request.path, payload)
  const query = getQuery(request.method, payload, request.queryParams)
- const headers = getHeaders(request.init?.headers)
+ const headers = sendBody(request.method) ? getHeaders(request.init?.headers): new Headers(request.init?.headers)
  const url = request.baseUrl + path + query

  const init = {
    ...request.init,
    method: request.method.toUpperCase(),
    headers,
    body: getBody(request.method, payload),
  }

  return { url, init }
}

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