【发布时间】:2012-07-11 15:26:02
【问题描述】:
我正在用 java 编写一个测试程序来测试我与 django 中的 restfull api 的连接(准确地说是 djangorestframework)。 选项之一是使用 curl 测试 api。 从 shell 运行 curl 命令可以正常工作: 例如:
curl --show-error --request GET --header 'Accept: application/json' --user "user:pwd" http://127.0.0.1:8000/api/v1/
这很好地返回了 api 根 url 和 json 格式的帮助文本。
现在,当我尝试使用 ProcessBuilder 从 java 调用相同的内容时,我得到了这个答案:
{"detail": "You do not have permission to access this resource. You may need to login or otherwise authenticate the request."}
我使用的java代码是:
ProcessBuilder p=new ProcessBuilder("curl","--show-error", "--request","GET",
"--header","'Accept: application/json'", "--user","\"" + userName + ":" + password + "\"", getApiRootUrlString());
final Process shell = p.start();
因为我也通过以下方式捕获错误流:
InputStream errorStream= shell.getErrorStream();
InputStream shellIn = shell.getInputStream();
我知道他启动 curl 命令,因为其中一个选项出错会显示 curl 帮助文本。
我不确定调用它有什么区别,很确定它是同一个命令。
顺便说一句,'getApiRootUrlString()' 返回正确的 url:http://127.0.0.1:8000/api/v1/
【问题讨论】:
标签: java json curl processbuilder django-rest-framework