0

我正在使用 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

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

Now, if i do the same thing in Java :

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

I get this body on the server, which is the one i expected :

no=sense&this=makes

The headers are exactly the same in both case (Excepted, obviously for the body content-length), the only thing that change is the body.

What is wrong with my Jython code ?

4

1 回答 1

0

我相信在我第一次将 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
于 2016-03-10T10:22:50.600 回答