With golang rest api, I get a ready url image for the image and show the image, but I’m getting any image url in my own google drive or google, what could be the reason why my project can’t get an image?
POST and GET code
r.POST("/blog", func(c *gin.Context) {
var blog Blog
if err := c.ShouldBindJSON(&blog); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// insert blog into database
query := "INSERT INTO my_table (title, body, image, date) VALUES ($1, $2, $3, $4) RETURNING id"
var id int
err := db.QueryRow(query, blog.Title, blog.Body, blog.ImageURL, time.Now()).Scan(&id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create blog"})
return
}
c.JSON(http.StatusOK, blog)
})
// get blog by ID
r.GET("/blog/:id", func(c *gin.Context) {
var blog Blog
id := c.Param("id")
// get blog from database
err := db.Get(&blog, "SELECT * FROM my_table WHERE id=$1", id)
if err != nil {
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "blog not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get blog"})
}
return
}
c.JSON(http.StatusOK, blog)
})
app.js:
function App() {
const [post, setPost] = useState(null);
const [error, setError] = useState(null);
useEffect(() => {
async function fetchData() {
try {
const response = await axios.get('http://localhost:8080/blog/5'); // replace "id" with the desired id
setPost(response.data);
} catch (error) {
setError(error.message);
}
}
fetchData();
}, []);
if (error) {
return <div>{error}</div>;
}
if (!post) {
return <div>Loading...</div>;
}
return (
<div>
<h1>{post.title}</h1>
<p>{post.body}</p>
<img src={post.image} alt={post.title} />
<p>{new Date(post.date).toLocaleString()}</p>
<p>ID: {post.id}</p>
</div>
);
}