简体   繁体   中英

Google Pub/Sub Emulator - Publishing message using Java's HTTP.POST throws 400 Bad Request

I have the Google Pub/Sub emulator running locally on my machine. I am able to publish a message using YARC (Yet Another REST Client) with the following details:

URL: POST http://localhost:8311/v1/projects/my-project/topics/my-project-test:publish

Payload:

{
  "messages": [
    {
      "attributes": {
        "1": 1
      }
    }
  ]
}

This results in a 200 response, and I can see in the console output from the emulator that the messages are received.

Now, when I try to do the same using Java's HTTP library, I am consistently getting 400 errors. Here's the code:

String payload = "{\"messages\":[{\"attributes\":{\"1\":1}}]}";

HTTP.Response response = HTTP.POST(options.getPubsubRootUrl() + "/v1/projects/my-project/topics/my-project-test:publish", payload);

I checked the headers in the debugger and found that they are:

Content-Type: application/json

Content-length: ####(some number)

I'm sending Content-Type from the REST client as well ( Content-length may be being sent behind the scenes as well)

As far as I can tell these requests are identical, but for some reason doing it from my Java code is causing an error while doing it from a REST client is not.

Is there anything glaring here I'm missing?

Turns out the HTTP class I thought was a standard Java class was org.neo4j.test.server.HTTP , which was auto-imported in Eclipse when I copied that line from elsewhere in my code.

That particular implementation is, I believe, designed to work with a neo4j test server and must do something beyond simple HTTP requests (add some headers or something), resulting in the requests being somehow different than what I was sending from YARC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM