【问题标题】:Unirest for JavaJava 的 Unirest
【发布时间】:2016-04-21 17:35:27
【问题描述】:

我有一个 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();

【问题讨论】:

  • 请具体说明它给您的错误是什么。是否有错误提示

标签: java python unirest


【解决方案1】:

如果没有更多信息,我认为这是您的引号位置的一个简单错误。

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

应该是

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

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

【讨论】:

  • 引号确实丢失了。感谢您指出。但我的问题是这将作为脚本工作
  • 我不知道。尝试编译它,看看它是否有效。
  • @sromit 让我知道这是否解决了您的问题,
  • 代码编译正常,但是服务器端还有其他问题,一旦解决,我会告诉你,我的unirest代码是否有效
  • unirest 代码不起作用..但是如果我用下面的代码替换(尽管是 diff 方法)就可以了 RestTemplate restTemplate = new RestTemplate();最终字符串 uri ="http://?????????????/ping"; HttpHeaders 标头 = 新的 HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); HttpEntity entity = new HttpEntity("参数", headers); ResponseEntity 结果 = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class); System.out.println(结果);
猜你喜欢
  • 2020-06-09
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 2020-05-19
  • 2013-11-02
  • 2020-02-03
  • 2020-04-12
相关资源
最近更新 更多