连接到Go RPC使用curl

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

Connecting to Go RPC using curl

问题

我使用了这里列出的基本指令创建了一个RPC服务器:https://golang.org/pkg/net/rpc/。我正在构建一个可能不是用Go语言编写的应用程序,用于与RPC服务器通信,并且想知道如何使用curl或其他方式手动连接到服务器。

我尝试过:

curl -v -X CONNECT --url localhost:1234/_goRPC

但是我得到的输出是:

* Hostname was NOT found in DNS cache
*   Trying ::1...
* Connected to localhost (::1) port 1234 (#0)
> CONNECT /_goRPC HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:1234
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< Date: Tue, 29 Sep 2015 15:04:29 GMT
< Content-Length: 19
<
404 page not found
* Connection #0 to host localhost left intact

这主要是为了更好地理解如何以语言无关的方式连接到它,在将来,我很可能会使用任何语言的库与服务器进行通信。

编辑:对于上述错误,URL实际上是localhost:1234/goRPC

但问题仍然是如何创建一个curl请求,执行与链接https://golang.org/pkg/net/rpc/中客户端执行的相同功能。方法调用和参数如何被序列化并放置在CONNECT请求中?

英文:

I created a RPC server using the basic instructions listed here: https://golang.org/pkg/net/rpc/. I am building a application that might not be in Go to communicate with a RPC server and was curious how to manually connect to the server using curl or anything else.

I have tried:

curl -v -X CONNECT --url localhost:1234/_goRPC

but the output I get is:

* Hostname was NOT found in DNS cache
*   Trying ::1...
* Connected to localhost (::1) port 1234 (#0)
> CONNECT /_goRPC HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:1234
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< Date: Tue, 29 Sep 2015 15:04:29 GMT
< Content-Length: 19
<
404 page not found
* Connection #0 to host localhost left intact

This is mainly more to understand how to connect to it language agnostically, in the future I will most likely be using libraries from whatever language to communicate to the server.

Edit: for the above error, the url is actually localhost:1234/goRPC

But the question still remains to how to create a curl request that performs the same functionality as in the link https://golang.org/pkg/net/rpc/ that the client performs. How does the method call and arguments get serialized and placed in the CONNECT request

答案1

得分: 0

对于任何想知道的人,使用rpc/jsonrpc库中的JSONRPC更容易,curl请求如下所示:

curl -X CONNECT --url <url>/_goRPC_ -d '{"method":"your rpc method","params":["args"],"id":<some number to represent your request>}'
英文:

For anyone wondering, it is easier to use JSONRPC from the rpc/jsonrpc library and the curl request looks like:

curl -X CONNECT --url &lt;url&gt;/_goRPC_ -d &#39;{&quot;method&quot;:&quot;your rpc method&quot;,&quot;params&quot;:[&quot;args&quot;],&quot;id&quot;:&lt;some number to represent your request&gt;}&#39;

huangapple
  • 本文由 发表于 2015年9月29日 23:07:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/32847561.html
匿名

发表评论

匿名网友

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

确定