简体   繁体   English

Java Unirest 将带有空 JSON 的 POST 请求发送到在本地主机上运行的服务器,但是在向云服务器发送相同的请求时工作正常?

[英]Java Unirest sends POST request with empty JSON to server running on localhost but works fine when sending the same request to cloud server?

I am building an agent in Java which has to solve games using a planner.我正在 Java 中构建一个代理,它必须使用计划器来解决游戏。 The planner that I am using runs as a service on the cloud, and thus anybody can send HTTP requests to it and get a response.我使用的规划器在云上作为服务运行,因此任何人都可以向它发送 HTTP 请求并获得响应。 I have to send to it a JSON with the following content: {"domain": "string containing the domain's description", "problem": "string containing the problem to be solved"} .我必须向它发送一个JSON ,其内容如下: {"domain": "string containing the domain's description", "problem": "string containing the problem to be solved"} As a response I get a JSON that contains the status and the result, which might be a plan or not, depending on whether there was some problem or not.作为回应,我得到一个JSON ,其中包含状态和结果,这可能是一个计划或不是一个计划,这取决于是否存在问题。

The following piece of code allows me to call the planner and receive its response, retrieving the JSON object from the body:以下代码允许我调用规划器并接收其响应,从正文中检索 JSON object:

String domain = readFile(this.gameInformation.domainFile);
String problem = readFile("planning/problem.pddl");

// Call online planner and get its response
String url = "http://solver.planning.domains/solve";
HttpResponse<JsonNode> response = Unirest.post(url)
    .header("accept", "application/json")
    .field("domain", domain)
    .field("problem", problem)
    .asJson();

// Get the JSON from the body of the HTTP response
JSONObject responseBody =  response.getBody().getObject();

This code works pefectly fine and I don't have any kind of problem with it.这段代码工作得很好,我没有任何问题。 Since I have to do some heavy testing on the agent, I prefer to run the server on localhost, so that the service doesn't get saturated (it can only process one request at a time).由于我必须对代理进行一些繁重的测试,我更喜欢在 localhost 上运行服务器,这样服务就不会饱和(一次只能处理一个请求)。

However, if I try to send a request to the server running on localhost, the body of the HTTP request that the server receives is empty.但是,如果我尝试向 localhost 上运行的服务器发送请求,则服务器收到的 HTTP 请求的主体为空。 Somehow, the JSON is not sent and I am receiving a response that contains an error.不知何故,JSON 未发送,我收到包含错误的响应。

The following piece of code illustrates how I am trying to send a request to the server running on localhost:以下代码说明了我如何尝试向在 localhost 上运行的服务器发送请求:

// Call online planner and get its response
String url = "http://localhost:5000/solve";
HttpResponse<JsonNode> response = Unirest.post(url)
    .header("accept", "application/json")
    .field("domain", domain)
    .field("problem", problem)
    .asJson();

For the sake of testing, I had previously created a small Python script that sends the same request to the server running on localhost:为了测试,我之前创建了一个小的 Python 脚本,它将相同的请求发送到在 localhost 上运行的服务器:

import requests

with open("domains/boulderdash-domain.pddl") as f:
    domain = f.read()

with open("planning/problem.pddl") as f:
    problem = f.read()

data = {"domain": domain, "problem": problem}

resp = requests.post("http://127.0.0.1:5000/solve", json=data)
print(resp)
print(resp.json())

When executing the script, I get a correct response, and it seems that the JSON is sent correctly to the server.执行脚本时,我得到了正确的响应,似乎 JSON 已正确发送到服务器。

Does anyone know why this is happening?有谁知道为什么会这样?

Okay, fortunately I have found an answer for this issue (don't try to code/debug at 2-3AM folks, it's never going to turn out right).好的,幸运的是我找到了这个问题的答案(不要尝试在凌晨 2 点到 3 点进行编码/调试,这永远不会正确)。 It seems that the problem was that I was specifying what kind of response I was expecting to get from the server instead of what I was trying to send to it in the request's body:似乎问题在于我指定了我期望从服务器获得的响应类型,而不是我试图在请求正文中发送给它的响应:

HttpResponse response = Unirest.post(url).header( "accept" , "application/json")... HttpResponse 响应 = Unirest.post(url).header( "accept" , "application/json")...

I was able to solve my problem by doing the following:我能够通过执行以下操作来解决我的问题:

// Create JSON object which will be sent in the request's body
JSONObject object = new JSONObject();
object.put("domain", domain);
object.put("problem", problem);

String url = "http://localhost:5000/solve";

<JsonNode> response = Unirest.post(url)
    .header("Content-Type", "application/json")
    .body(object)
    .asJson();

Now I am specifying in the header what type of content I am sending.现在我在 header 中指定我要发送的内容类型。 Also, I have create a JSONObject instance that contains the information that will be added to the request's body.此外,我创建了一个JSONObject实例,其中包含将添加到请求正文中的信息。 By doing this, it works on both the local and cloud servers.通过这样做,它可以在本地和云服务器上运行。

Despite of this, I still don't really get why when I was calling the cloud server I was able to get a correct response, but it doesn't really matter now.尽管如此,我仍然不明白为什么当我调用云服务器时我能够得到正确的响应,但现在这并不重要。 I hope that this answer is helpful for someone who is facing a similar issue!我希望这个答案对面临类似问题的人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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