2

我有一个 Python 脚本,它将数据发布到服务器 url,如下所示。我想使用 unirest for Java 来实现这一点。如何在 Java 中为 unirest post 请求添加参数/标头值

Python脚本:

url = 'http://??????????????????/SaveTimeSeriesData'
        params = {'clientId': 'admin', 'tenantId': '075841cb-d7fa-4890-84ea-fdd7d7c65b65', 'destinationId': 'TimeSeries','content-type': 'application/json','content':json.dumps(HTLT_DATA.__dict__)}
      try:
            response= requests.post(url, params=params, proxies=proxies)
            print response 
        except Exception as x:
            print x    

服务器端点代码是:

@RequestMapping(value = "/SaveTimeSeriesData", method = RequestMethod.POST)
public @ResponseBody String saveMachineData(
        @RequestHeader(value = "authorization", required = false) String authorization,
        @RequestParam("clientId") String clientId, @RequestParam("tenantId") String tenantId,
        @RequestParam("content") String content)
{

java中最统一的代码是什么?下面的代码是否会达到与 python 脚本相同的结果

    Unirest.post("http://???????????????/SaveTimeSeriesData")
        .field("content", mo)
        .field("tenantId", "075841cb-d7fa-4890-84ea-fdd7d7c65b65")
        .field("clientID","admin")
        .field("content-type","application/json")                   
            .asJson();
4

1 回答 1

1

如果没有更多信息,我相信这是您报价位置的一个简单错误。

      .field("tenantId, "075841cb-d7fa-4890-84ea-fdd7d7c65b65")

真的应该

      .field("tenantId", "075841cb-d7fa-4890-84ea-fdd7d7c65b65")

请注意,租户 ID 没有结束报价。整行可能已被视为字符串文字。让我知道这是否修复了错误

于 2016-01-16T02:11:54.410 回答