Skip to content

Commit

Permalink
Error on json/text GET submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jun 8, 2023
1 parent f8a71dc commit 2b586c9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,11 @@ function normalizeNavigateOptions(
};
}

let getInvalidBodyError = () => ({
path,
error: getInternalRouterError(400, { type: "invalid-body" }),
});

// Create a Submission on non-GET navigations
let rawFormMethod = opts.formMethod || "get";
let formMethod = normalizeFormMethod
Expand All @@ -3126,6 +3131,11 @@ function normalizeNavigateOptions(

if (opts.body) {
if (opts.formEncType === "text/plain") {
// text only support POST/PUT/PATCH/DELETE submissions
if (!isMutationMethod(formMethod)) {
return getInvalidBodyError();
}

let text =
typeof opts.body === "string"
? opts.body
Expand All @@ -3150,6 +3160,11 @@ function normalizeNavigateOptions(
},
};
} else if (opts.formEncType === "application/json") {
// json only supports POST/PUT/PATCH/DELETE submissions
if (!isMutationMethod(formMethod)) {
return getInvalidBodyError();
}

try {
let json =
typeof opts.body === "string" ? JSON.parse(opts.body) : opts.body;
Expand All @@ -3166,10 +3181,7 @@ function normalizeNavigateOptions(
},
};
} catch (e) {
return {
path,
error: getInternalRouterError(400, { type: "invalid-body" }),
};
return getInvalidBodyError();
}
}
}
Expand Down Expand Up @@ -3199,10 +3211,7 @@ function normalizeNavigateOptions(
searchParams = new URLSearchParams(opts.body);
formData = convertSearchParamsToFormData(searchParams);
} catch (e) {
return {
path,
error: getInternalRouterError(400, { type: "invalid-body" }),
};
return getInvalidBodyError();
}
}

Expand Down

0 comments on commit 2b586c9

Please sign in to comment.