没有从Jython正确发送的Unirest表单字段

时间:2016-03-10 09:55:09

标签: java http jython unirest

我正在使用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代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

我相信,当我第一次将它们作为一个字段传递时,jython字符串在某种程度上被视为Unirest的集合。我不确定为什么,但是使用这种直觉,我为我的Jython代码管理了一个解决方法。

明确地将Jython String转换为Java String,据我所知通常不需要的东西修复了我的问题。

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