Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CalebM1987 opened this issue Apr 1, 2022 · 0 comments · May be fixed by #20
Closed

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

CalebM1987 opened this issue Apr 1, 2022 · 0 comments · May be fixed by #20

Comments

@CalebM1987
Copy link

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 }
}
@CalebM1987 CalebM1987 changed the title Don't send application\json content-type header with empty body on Don't send application\json content-type header with empty body on GET requests Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant