mrjack
mrjack11mo ago

charset=utf8 results in empty input

I'm using trpc-openapi and some of our customers are using Zapier to communicate with it. Zapier uses the header Content-Type: application/json; charset=utf-8 - note the additional charset=utf-8 When charset=utf-8 exists, the input to the relevant procedure is {} Has anybody encountered this? Is there a way to support it?
14 Replies
mrjack
mrjack11mo ago
Note I'm using adapters/aws-lambda
sachin
sachin11mo ago
i think i know why this is happening
mrjack
mrjack11mo ago
@sachinraja - a penny for your thoughts? I found a few string comparisons to "application/json" in the codebase - I thought that might be it
sachin
sachin11mo ago
yeah ill do a PR - should probably be doing .includes instead sorry i meant to follow up on this lol
mrjack
mrjack11mo ago
Thanks @sachinraja ❤️
sachin
sachin11mo ago
can i ask you to try something with a patch (patch-package)?
mrjack
mrjack11mo ago
I'm just in a meeting right now - I'd be happy to give it a crack in about 1h
sachin
sachin11mo ago
awesome 👍
mrjack
mrjack11mo ago
n.b. I'm on Yarn 3 which has it's own patch package system. I can just do a patch manually though if need be
sachin
sachin11mo ago
ok cool ill see if i can do a canary publish actually I thought I knew why this was happening but now I do not. I just read that you're using the aws lambda adapter so that avoids a lot of potential issues I thought this could be related to. Also not sure why the input comes through as an empty object. Might be some aws specific stuff happening
mrjack
mrjack11mo ago
I'm back now So here's a wrapper function I'm using to hack the adapter
import { APIGatewayProxyHandlerV2 } from "aws-lambda";

const charsetString = `; charset=utf-8`;


/** a filthy hack to workaround requests from Zapier where header is
* `Content-Type: application/json; charset=utf-8` */
export const hackContentType =
(fn: APIGatewayProxyHandlerV2): APIGatewayProxyHandlerV2 =>
(event, context, callback) => {
const contentType = event.headers["content-type"];
if (contentType?.includes(charsetString)) {
event.headers["content-type"] = contentType
.replace(charsetString, "")
.trim();
}

return fn(event, context, callback);
};
import { APIGatewayProxyHandlerV2 } from "aws-lambda";

const charsetString = `; charset=utf-8`;


/** a filthy hack to workaround requests from Zapier where header is
* `Content-Type: application/json; charset=utf-8` */
export const hackContentType =
(fn: APIGatewayProxyHandlerV2): APIGatewayProxyHandlerV2 =>
(event, context, callback) => {
const contentType = event.headers["content-type"];
if (contentType?.includes(charsetString)) {
event.headers["content-type"] = contentType
.replace(charsetString, "")
.trim();
}

return fn(event, context, callback);
};
I also noted that the comparisons to "application/json" didn't exist in the aws-lambda adapter But as you can see in this wrapper, adjusting event.headers["content-type"] causes createOpenApiAwsLambdaHandler to behave correctly Usage in case it's not clear
export const handler: APIGatewayProxyHandlerV2 = hackContentType(
createOpenApiAwsLambdaHandler({
router: appRouter,
createContext,
})
);
export const handler: APIGatewayProxyHandlerV2 = hackContentType(
createOpenApiAwsLambdaHandler({
router: appRouter,
createContext,
})
);
sachin
sachin11mo ago
interesting quite an annoying bug i don’t think i can get around to fixing this soon anymore (will be gone for about 2 weeks) but i’ll still keep it in mind but feel free to create an issue on github, hopefully someone else will pick it up
mrjack
mrjack11mo ago
Thanks @sachinraja
mrjack
mrjack11mo ago
Created an issue on Github https://github.com/trpc/trpc/issues/4783
GitHub
bug: charset=utf8 results in empty input · Issue #4783 · trpc/trpc
Provide environment information Redacted due to privacy and it's not relevant to the bug Describe the bug I'm using trpc-openapi and adapters/aws-lambda. Some of our customers are using Zap...