Django-Piston 和 Python 客户端与 Java 客户端

发布于 2024-10-21 17:10:51 字数 1291 浏览 8 评论 0原文

我使用 Django-Piston 构建了一个 Web 服务,允许 POST 和 GET 请求。作为测试的一部分,我编写了一个快速的 Python 脚本。使用该脚本我可以成功执行两种类型的请求;但是,当用 Java 编写的客户端尝试执行 POST 时,我收到错误: "POST /api/service/ HTTP/1.1" 400 225 "-" "Apache-HttpClient/4.1 (java 1.5)"

我的理解是 http任何语言生成的请求消息都必须相同。换句话说,如果我使用 python 客户端测试我的 Web 服务并且它可以工作,那么它应该适用于具有 http 库的所有其他语言。

这是 POST 的 python 代码:

 import urllib, urllib2

 data = urllib.urlencode({'url': 'www.uvic.ca', 'name': 'uvic'})
 url = 'http://xxx/api/service/'
 req = urllib2.Request(url, data)

 print urllib2.urlopen(req).read() 

这是 Java 代码:

HttpPost httpost = new HttpPost("http://xxx/api/service/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "some_name"));
nvps.add(new BasicNameValuePair("url", "www.somename.com"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();

System.out.println(entity.getContentType());
System.out.println(EntityUtils.getContentCharSet(entity));
System.out.println(EntityUtils.toString(entity));

我开始认为这是一个 apache 配置问题。我在 POST 方法的开头添加了一些调试语句,但我根本没有点击它们。这意味着 urls.py 文件有问题(我对此表示怀疑,因为它在 python 中工作)或者 apache 有问题。

请帮忙。提前致谢。

I've built a web service using Django-Piston that allows for POST and GET requests. As part of testing I wrote a quick Python script. Using the script I can successfully do both types of requests; however, when a client written in Java attempts to do a POST I get an error: "POST /api/service/ HTTP/1.1" 400 225 "-" "Apache-HttpClient/4.1 (java 1.5)"

My understanding is that http request message that is generated by any language has to be the same. In other words, if I test my web service using a python client and it works then it should work for all other languages that have a http library.

Here is the python code for the POST:

 import urllib, urllib2

 data = urllib.urlencode({'url': 'www.uvic.ca', 'name': 'uvic'})
 url = 'http://xxx/api/service/'
 req = urllib2.Request(url, data)

 print urllib2.urlopen(req).read() 

and here is the Java code:

HttpPost httpost = new HttpPost("http://xxx/api/service/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "some_name"));
nvps.add(new BasicNameValuePair("url", "www.somename.com"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();

System.out.println(entity.getContentType());
System.out.println(EntityUtils.getContentCharSet(entity));
System.out.println(EntityUtils.toString(entity));

I'm starting to think that this is an apache configuration problem. I put in some debugging statements at the start of my method for POST and I'm not hitting them at all. This means that something is wrong with the urls.py file (which I doubt because it works in python) or something is weird with apache.

Please help. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-10-28 17:10:51

一点点搜索会对你有很大帮助。这是第一个谷歌结果。

http://weblog.mattdorn。 com/content/restful-web-apps-with-django-piston-and-ext-js/

400错误的原因是
ExtJS 正在将一个字符集附加到
Content-Type 字段,这会导致
活塞不解释
内容类型正确。有一个
其开放问题位于
http://bitbucket.org /jespern/django-piston/issue/121/content-type-is-not-being-split-against
我能够让这个例子工作
在我应用补丁并做了之后
轻松安装。

这是谷歌的第二次回应。

https://bitbucket.org /jespern/django-piston/issue/99/bad-request-and-content-type-with-fix

A little search would help you a lot. This was the first Google result.

http://weblog.mattdorn.com/content/restful-web-apps-with-django-piston-and-ext-js/

The reason for the 400 errors is that
ExtJS is appending a charset onto the
Content-Type field, which causes
piston to not interpret the
Content-Type correcly. There is an
open issue for it at
http://bitbucket.org/jespern/django-piston/issue/121/content-type-is-not-being-split-against.
I was able to get the example working
after I applied the patch and did an
easy_install.

This is the second Google response.

https://bitbucket.org/jespern/django-piston/issue/99/bad-request-and-content-type-with-fix

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文