gpt4 book ai didi

FetchError: invalid json response body GraphQL(FetchError:无效的json响应体GraphQL)

转载 作者:bug小助手 更新时间:2023-10-22 15:10:13 26 4
gpt4 key购买 nike



I'm using @apollo/datasource-rest to wrap a 3rd party API. The endpoint I'm trying to hit checks if an email is already in use or not. If the email is in use the response looks like this

我正在使用@apolo/datasource-rest来包装第三方API。我尝试访问的端点检查电子邮件是否已在使用中。如果电子邮件正在使用,则回复如下


    "body": "True",
"statusCode": 200,
"contentType": "application/json",

If the email is not in use the api returns a 404 like this

如果电子邮件未被使用,api将返回404,如下所示


    "body": "",
"statusCode": 404,
"statusDescription": "Resource not found",

The problem is the express.json() or bodyParser.json() middleware throws an error because "True" is not valid JSON and it is only getting parsed as json because the 3rd party API is sending back a response header of "Content-Type": "application/json".

问题是express.json()或bodyParser.json()中间件抛出错误,因为“True”不是有效的json,它只被解析为json,因为第三方API正在发送回“Content-Type”的响应头:“application/json”。


Is there a way to change the content type of the response before it hits the express.json() or bodyParser.json() middleware

有没有办法在响应到达express.json()或bodyParser.json()中间件之前更改响应的内容类型


Here is my Apollo Express Server

这是我的阿波罗快车服务器


const main = async () => {
const app = express()
const httpServer = http.createServer(app)
const port = process.env.PORT

const schema = makeExecutableSchema({ resolvers, typeDefs })

// Set up Apollo Server
const server = new ApolloServer<MyContext>({
schema,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
})

await server.start()

app.use(
'/graphql',
cors<cors.CorsRequest>({
origin: '*',
}),
json(),
expressMiddleware(server, {
context: async ({ req, res }) => {
return {
req,
res,
datasources: {
someAPI: new SomeAPI(),
},
}
},
}),
)

await new Promise<void>((resolve) => httpServer.listen({ port }, resolve))
console.log(`🚀 Server ready at http://localhost:${port}/graphql`)
}

main().catch((error) => {
console.error(error)
})

更多回答

You're right that the 3rd party API is lying about its response being JSON, now what to do about it? Please show the code where you're using @apollo/datasource-rest and where you're querying the 3rd party API.

你说得对,第三方API谎称其响应为JSON,现在该怎么办?请显示您使用@apolo/datasource-rest的位置以及查询第三方API的位置。

优秀答案推荐
更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com