【问题标题】:HTTP Post method using curl to post data to localhost failed使用 curl 将数据发布到 localhost 的 HTTP Post 方法失败
【发布时间】:2014-05-14 20:28:40
【问题描述】:

我正在尝试使用 HTTP GET 方法:

curl http://localhost:8888/_ah/api/birra/v1/beer

它返回给我的当前列表是正常和预期的:

{
  "items" : [ {
    "id" : "1",
    "beerName" : "Bud"
  }, {
    "id" : "2",
    "beerName" : "Steve"
  }, {
    "id" : "3",
    "beerName" : "Ankur"
  } ]
}

但是当我像这样进行 HTTP POST 时:

curl -X POST -H "Content-Type: application/json" -H "Accept:application/json" -d "{\"beerName\": \"asdf\"}" http://localhost:8888/_ah/api/birra/v1/beer

它给我的错误是:

{
  "error" : {
    "message" : "javax.jdo.JDOFatalInternalException: The key value passed to construct a SingleFieldIdentity of type \"class javax.jdo.identity.LongIdentity\" for class \"class com.samples.Beer\" is null.\nNestedThrowables:\norg.datanucleus.exceptions.NucleusException: The key value passed to construct a SingleFieldIdentity of type \"class javax.jdo.identity.LongIdentity\" for class \"class com.samples.Beer\" is null.",
    "code" : 503,
    "errors" : [ {
      "domain" : "global",
      "reason" : "backendError",
      "message" : "javax.jdo.JDOFatalInternalException: The key value passed to construct a SingleFieldIdentity of type \"class javax.jdo.identity.LongIdentity\" for class \"class com.samples.Beer\" is null.\nNestedThrowables:\norg.datanucleus.exceptions.NucleusException: The key value passed to construct a SingleFieldIdentity of type \"class javax.jdo.identity.LongIdentity\" for class \"class com.samples.Beer\" is null."
    } ]
  }
}

我目前正在研究提供的 Google Cloud Endpoints 示例: Cloud Endpoint by Google Developers

我正在使用 Eclipse IDE 和在 Jetty 上运行的服务器 localhost。

有什么解决办法吗?

【问题讨论】:

  • 输入 JSON 中不需要 id 吗?
  • 不,我在我的代码中使用“beerName”作为键,所以我将一个值作为“asdf”传递给它。你在哪里使用id?另外,请查看我提供的链接中的代码,它与教程中的内容相同。
  • 只是一个疯狂的猜测.. 异常包含javax.jdo.identity.LongIdentity。我认为LongIdentity 意味着它必须与id 相关,因为它必须是Java long
  • 您确定即使不提供id 也会构造一个新的Beer 对象?另外,据我了解您链接中的代码,我认为id 是关键,而不是beerName。如果您看到 getBeer 服务端点,它正在接受 id 而不是 beerName
  • 不,在 getBeer() 中,它所采用的 'id' 是从服务器检索数据。如果我们这样做: curl localhost:8888/_ah/api/birra/v1/beer/1 它将返回带有'id' = '1'的实体请查看insertBeer(),我只会在做 HTTP POST。 HTTP GET 工作正常。

标签: java google-app-engine curl jdo google-cloud-endpoints


【解决方案1】:

使用双引号代替单引号。当您从 Windows 运行时会发生此类错误。这就是为什么它说couldn't resolve host

curl  -H "Content-Type: application/json" -d "{\"beerName\": \"bud\"}" http://localhost:8888/_ah/api/birra/v1/beer

【讨论】:

  • curl -X POST -H "Content-Type: application/json" -H "Accept:application/json" -d "{\"beerName\": \"asdf\"}" http://localhost:8888/_ah/api/birra/v1/beer { "error" : { "message" : "javax.jdo.JDOFatalInternalException: The key value passed to construct a SingleFieldIdentity of type \"class javax.jdo.identity.LongIdentity\" for class \"class com.samples.Beer\" is null.\nNestedThrowables:\norg.datanucleus.exceptions.NucleusException: The key value passed to construct a SingleFieldIdentity of type \"class javax.jdo.identity.LongIdentity\" for class \"class com.samples.Beer\" is null."
  • 我使用了这个命令,所以无法解析主机错误消失了,但主要错误仍然存​​在。请帮忙。提前致谢。
  • @user3275​​261 是服务器端的问题。可能是您错误地解析了 json 并且某些字段为空,只是猜测。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 2018-01-17
  • 2014-09-21
相关资源
最近更新 更多