【问题标题】:Unirest form fields not being sent correctly from Jython未从 Jython 正确发送 Unirest 表单字段
【发布时间】:2016-06-25 02:16:39
【问题描述】:

我正在使用 Jython (2.7) 和 Java (7) 代码中的 java Unirest (1.4.7) 实现。

我在从 Jython 代码发送 http 请求时遇到了问题:

这是 Jython 代码:

import com.mashape.unirest.http.Unirest as Unirest;
r = Unirest.post("http://localhost:5002/test").field(u"this", u"makes").field(u"no", u"sense").asString();

当我在服务器端打印它时,这给了我以下请求正文:

no=sense&this=m&this=a&this=k&this=e&this=s

第一个“字段”总是“分散”在请求正文中,就好像它是一个集合一样。

现在,如果我在 Java 中做同样的事情:

try {
    Unirest.post("http://localhost:5002/test")
    .field("this", "makes")
    .field("no", "sense")
    .asString();
} catch (UnirestException e) {
    e.printStackTrace();
}

我在服务器上得到了这个身体,这是我所期望的:

no=sense&this=makes

两种情况下的标头完全相同(除了,显然是正文内容长度),唯一改变的是正文。

我的 Jython 代码有什么问题?

【问题讨论】:

    标签: java http jython unirest


    【解决方案1】:

    我相信,当我第一次将 jython 字符串作为字段传递时,Unirest 会以某种方式将它们视为集合。我不太清楚为什么,但使用这种直觉,我为我的 Jython 代码管理了一个解决方法。

    将 Jython 字符串显式转换为 Java 字符串,据我所知,这通常不是必需的,解决了我的问题。

    import com.mashape.unirest.http.Unirest as Unirest;
    import java.lang.String as jstr
    
    Unirest.post("http://localhost:5002/test").field(jstr(u"this"), jstr(u"makes")).field(jstr(u"no"), jstr(u"sense")).asString();
    

    现在生成预期的请求正文:

    no=sense&this=makes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2015-04-26
      • 1970-01-01
      • 2020-07-09
      • 2019-12-28
      • 1970-01-01
      相关资源
      最近更新 更多