如何解决Golang curl 找不到接受参数'\'的位置参数
我正在根据 golang 网站的教程制作一个基本的 golang 应用程序,并将最终代码粘贴到此处。
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// album represents data about a record ablum
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
// albums slice to seed record album data.
var albums = []album{
{ID: "1",Title: "Blue Train",Artist: "John Coltrane",Price: 56.99},{ID: "2",Title: "Jeru",Artist: "Gerry Mulligan",Price: 17.99},{ID: "3",Title: "The Suburbs",Artist: "Arcade Fire",Price: 39.99},}
//getAlbums response with the list of all albums as JSON
func main() {
router := gin.Default()
router.GET("/albums",getAlbums)
router.GET("/albums/:id",getAlbumByID)
router.POST("/albums",postAlbums)
router.Run("localhost:8081")
}
func getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK,albums)
}
func postAlbums(c *gin.Context) {
var newAlbum album
// Call BindJSON to bind the received JSON to
// newAlbum.
if err := c.BindJSON(&newAlbum); err != nil {
return
}
// Add the new album to the slice.
albums = append(albums,newAlbum)
c.IndentedJSON(http.StatusCreated,newAlbum)
}
// getAlbumByID locates the album whose ID value matches the id
// parameter sent by the client,then returns that album as a response.
func getAlbumByID(c *gin.Context) {
id := c.Param("id")
// Loop over the list of albums,looking for
// an album whose ID value matches the parameter.
for _,a := range albums {
if a.ID == id {
c.IndentedJSON(http.StatusOK,a)
return
}
}
c.IndentedJSON(http.StatusNotFound,gin.H{"message": "album not found"})
}
它将专辑存储在内存中。我正在尝试使用 POST 请求,通过在 powershell 中使用 curl 命令来添加新专辑。这是添加项目函数:https://golang.org/doc/tutorial/web-service-gin#add_item
我的问题是当我运行 curl 命令添加新专辑时,如文章所述,它给了我以下消息:
Invoke-WebRequest:找不到接受参数“”的位置参数。 在行:1 字符:1
- curl http://localhost:8081/albums \ --include \ --header "Content-Typ ...
-
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
我该怎么做才能获得添加新专辑的 POST 请求?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。