如何使用 pubsub 模拟器 http API 创建订阅?

How to create subscription using pubsub emulator http API?

启动 pubsub 模拟器后,我尝试使用 HTTP API 创建主题和订阅。创建主题成功,但我不明白为什么创建订阅不成功。我做错了什么或者这是工具中的错误?您可以看到以下日志:

$ curl -s -X PUT http://localhost:8085/v1/projects/myproject/topics/mytopic
{
  "name": "projects/myproject/topics/mytopic"
}

$ curl -s -X PUT http://localhost:8085/v1/projects/myproject/subscriptions/mysub \
    --data '{"topic":"projects/myproject/topics/mytopic"}'
Not Found

在模拟器端,我看到以下内容:

# create topic logs
[pubsub] Apr 29, 2020 10:37:19 AM io.gapi.emulators.grpc.GrpcServer operationComplete
[pubsub] INFO: Adding handler(s) to newly registered Channel.
[pubsub] Apr 29, 2020 10:37:19 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFO: Detected non-HTTP/2 connection.
[pubsub] Apr 29, 2020 10:37:19 AM io.gapi.emulators.grpc.GrpcServer operationComplete
[pubsub] INFO: Adding handler(s) to newly registered Channel.
[pubsub] Apr 29, 2020 10:37:19 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFO: Detected HTTP/2 connection.

# create subscription logs
[pubsub] Apr 29, 2020 10:37:27 AM io.gapi.emulators.grpc.GrpcServer operationComplete
[pubsub] INFO: Adding handler(s) to newly registered Channel.
[pubsub] Apr 29, 2020 10:37:27 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFO: Detected non-HTTP/2 connection.
[pubsub] Apr 29, 2020 10:37:27 AM io.gapi.emulators.netty.NotFoundHandler handleRequest
[pubsub] INFO: Unknown request URI: /v1/projects/myproject/subscriptions/mysub

即使使用上述命令创建主题(不需要内容类型),要使用 --data '...' 选项发送数据,您还需要发送内容类型 header。所以下面的命令确实有效:

$ curl -s -X PUT http://localhost:8085/v1/projects/myproject/subscriptions/mysub \
    -H 'content-type: application/json' \
    --data '{"topic":"projects/myproject/topics/mytopic"}'