Golang重置服务的POST方法中的POST数据未定义。

huangapple go评论86阅读模式
英文:

Golang reset service post method post data undefined

问题

你好,以下是你提供的代码的翻译:

package main

import (
	"code.google.com/p/gorest"
	"fmt"
	"net/http"
)

type Invitation struct {
	User string
}

// 服务定义
type HelloService struct {
	gorest.RestService
	// gorest.RestService `root:"/tutorial/"`
	helloWorld gorest.EndPoint `method:"GET" path:"/hello-world/" output:"string"`
	sayHello   gorest.EndPoint `method:"GET" path:"/hello/{name:string}" output:"string"`
	posted     gorest.EndPoint `method:"POST" path:"/post/" postdata:"User"`
}

func main() {
	gorest.RegisterService(new(HelloService)) // 注册我们的服务
	http.Handle("/", gorest.Handle())
	http.ListenAndServe(":8787", nil)
}

func (serv HelloService) Posted(posted User) {
	fmt.Println(User)
}

func (serv HelloService) HelloWorld() string {
	return "Hello World"
}

func (serv HelloService) SayHello(name string) string {
	return "Hello " + name
}

这是你遇到的错误:

# command-line-arguments
./registation.go:28: undefined: User
./registation.go:29: undefined: User

请帮助我解决这个问题,谢谢。

英文:

hi i am working with google golang reset service i have issue with post method
when i try to run this code it show post data undefined

    package main

import (
	"code.google.com/p/gorest"
	"fmt"
	"net/http"
)

type Invitation struct {
	User string
}

//Service Definition
type HelloService struct {
	gorest.RestService
	//gorest.RestService `root:"/tutorial/"`
	helloWorld gorest.EndPoint `method:"GET" path:"/hello-world/" output:"string"`
	sayHello   gorest.EndPoint `method:"GET" path:"/hello/{name:string}" output:"string"`
	posted     gorest.EndPoint `method:"POST" path:"/post/"  postdata:"User" `
}

func main() {
	gorest.RegisterService(new(HelloService)) //Register our service
	http.Handle("/", gorest.Handle())
	http.ListenAndServe(":8787", nil)
}

func (serv HelloService) Posted(posted User) {
	fmt.Println(User)
}

func (serv HelloService) HelloWorld() string {
	return "Hello World"
}
func (serv HelloService) SayHello(name string) string {
	return "Hello " + name
}

this is the error i am getting

# command-line-arguments
./registation.go:28: undefined: User
./registation.go:29: undefined: User

please help to fix this issue
thank you

答案1

得分: 0

func (serv HelloService) Posted(posted User) {
fmt.Println(posted)
}

posted方法接受一个名为posted、类型为User的参数。

你应该打印实际的参数,而不是它的类型 - 就像你在这里做的一样

func (serv HelloService) SayHello(name string) string {
return "Hello " + name
}

英文:
func (serv HelloService) Posted(posted User) {
    fmt.Println(User)
}

should be

func (serv HelloService) Posted(posted User) {
    fmt.Println(posted)
}

The posted method takes a parameter with the name of posted and type of User.

You should be printing the actual paramater, not it's type - Just like you have here

func (serv HelloService) SayHello(name string) string {
    return "Hello " + name
}

答案2

得分: -1

package main

import (
	//"bytes"
	"code.google.com/p/gorest"
	"crypto/rand"
	"crypto/tls"
	//"encoding/json"
	"fmt"
	"log"
	"net"
	"net/http"
	"net/mail"
	"net/smtp"
)

type InvitEmail struct {
	SenderEmail   string
	ReceiverEmail string
}

//Service Definition
type HelloService struct {
	gorest.RestService
	//gorest.RestService `root:"/tutorial/"`
	helloWorld   gorest.EndPoint `method:"GET" path:"/hello-world/" output:"string"`
	sayHello     gorest.EndPoint `method:"GET" path:"/hello/{name:string}" output:"string"`
	userRegister gorest.EndPoint `method:"POST" path:"/UserRegister/" postdata:"InvitEmail"`
	/*activate     gorest.EndPoint `method:"GET" path:"/activate/{email:string}/{token:string}" output:"bool"`
	userInvite   gorest.EndPoint `method:"POST" path:"/UserInvite/" postdata:"InvitEmail"`*/
}

func main() {
	gorest.RegisterService(new(HelloService)) //Register our service
	http.Handle("/", gorest.Handle())
	http.ListenAndServe(":8787", nil)
}

func (serv HelloService) UserRegister(u InvitEmail) {

	fmt.Println(u.ReceiverEmail, "xxxxxxxx", u.SenderEmail)


	invitationmail(u.SenderEmail, u.ReceiverEmail)

	serv.ResponseBuilder().SetResponseCode(200).Write([]byte("from :" + u.SenderEmail + "      to :" + u.ReceiverEmail))
}

func (serv HelloService) HelloWorld() string {
	return "Hello World"
}
func (serv HelloService) SayHello(name string) string {
	
	return "Hello " + name
}
func invitationmail(sender, receiver) {
//emailsendingpart
}

这是一个Go语言的代码示例,包含了一个名为HelloService的服务和一些相关的函数。其中,UserRegister函数接收一个InvitEmail结构体作为参数,并调用invitationmail函数发送邮件。其他函数包括HelloWorldSayHello,分别返回字符串"Hello World"和"Hello"加上传入的名字。整个代码的作用是注册一个服务并监听端口8787,提供相应的API接口。

英文:
        package main
import (
//"bytes"
"code.google.com/p/gorest"
"crypto/rand"
"crypto/tls"
//"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/mail"
"net/smtp"
)
type InvitEmail struct {
SenderEmail   string
ReceiverEmail string
}
//Service Definition
type HelloService struct {
gorest.RestService
//gorest.RestService `root:"/tutorial/"`
helloWorld   gorest.EndPoint `method:"GET" path:"/hello-world/" output:"string"`
sayHello     gorest.EndPoint `method:"GET" path:"/hello/{name:string}" output:"string"`
userRegister gorest.EndPoint `method:"POST" path:"/UserRegister/" postdata:"InvitEmail"`
/*activate     gorest.EndPoint `method:"GET" path:"/activate/{email:string}/{token:string}" output:"bool"`
userInvite   gorest.EndPoint `method:"POST" path:"/UserInvite/" postdata:"InvitEmail"`*/
}
func main() {
gorest.RegisterService(new(HelloService)) //Register our service
http.Handle("/", gorest.Handle())
http.ListenAndServe(":8787", nil)
}
func (serv HelloService) UserRegister(u InvitEmail) {
fmt.Println(u.ReceiverEmail, "xxxxxxxx", u.SenderEmail)
invitationmail(u.SenderEmail, u.ReceiverEmail)
serv.ResponseBuilder().SetResponseCode(200).Write([]byte("from :" + u.SenderEmail + "      to :" + u.ReceiverEmail))
}
func (serv HelloService) HelloWorld() string {
return "Hello World"
}
func (serv HelloService) SayHello(name string) string {
return "Hello " + name
}
func invitationmail(sender, receiver) {
//emailsendingpart
}

huangapple
  • 本文由 发表于 2015年4月1日 12:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/29382755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定