你好 StackOverflow 社区,
我正在尝试从我的 React 应用程序向 Sanity Studio 发送发布请求。我想将一些用户数据发送到我的后端,但我不确定后端 URL 是什么。 ('http://localhost:3333/desk/user') 我从我想要获取此发布信息的 sanity studio 复制它。 这是我要发送的代码和用户数据:
import axios from 'axios';
import jwt_decode from 'jwt-decode';
export const createOrGetUser = async (response) => {
const decoded = jwt_decode(response);
const { name, picture, sub } = decoded;
const user = {
_id: sub,
_type: 'user',
userName: name,
image: picture,
}
console.log(user);
await axios.post('http://localhost:3333/desk/user', user);
}
我可以从 Google 获取用户数据,但我无法将其发送到我的后端。谁能帮我弄清楚后端 URL 是什么,或者我如何才能正确发送发布请求?
提前感谢您的帮助!
根据您提供的信息,您尝试将发布请求发送到的后端 URL 似乎是“http://localhost:3333/desk/user”。但是,请务必注意,此 URL 特定于您的本地开发环境,一旦您的应用程序部署到实时服务器上,它可能无法正常工作。
至于正确发送发布请求,您的代码看起来不错。但是,您可能希望添加一些错误处理来处理请求期间可能发生的任何潜在错误。这是一个例子: 从“axios”导入 axios; 从 'jwt-decode' 导入 jwt_decode;
export const createOrGetUser = async (response) => { const decoded = jwt_decode(响应);
const { name, picture, sub } = decoded;
const user = {
_id: sub,
_type: 'user',
userName: name,
image: picture,
}
console.log(user);
try {
const response = await axios.post('http://localhost:3333/desk/user', user);
console.log(response.data); // log the response data if the request is successful
} catch (error) {
console.error(error); // log any errors that occur during the request
}
}