使用 urllib2 进行 POST 调用而不是 GET

编程入门 行业动态 更新时间:2024-10-24 23:16:17
本文介绍了使用 urllib2 进行 POST 调用而不是 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

关于 urllib2 和 POST 调用有很多内容,但我遇到了一个问题.

There's a lot of stuff out there on urllib2 and POST calls, but I'm stuck on a problem.

我正在尝试对服务进行简单的 POST 调用:

I'm trying to do a simple POST call to a service:

url = 'myserver/post_service' data = urllib.urlencode({'name' : 'joe', 'age' : '10'}) content = urllib2.urlopen(url=url, data=data).read() print content

我可以看到服务器日志,当我发送数据时,它说我正在执行 GET 调用urlopen 的参数.

I can see the server logs and it says that I'm doing GET calls, when I'm sending the data argument to urlopen.

该库引发了 404 错误(未找到),这对于 GET 调用是正确的,POST 调用处理得很好(我也在尝试在 HTML 表单中使用 POST).

The library is raising an 404 error (not found), which is correct for a GET call, POST calls are processed well (I'm also trying with a POST within a HTML form).

推荐答案

这可能之前已经回答过:Python URLLib/URLLib2 POST.

This may have been answered before: Python URLLib / URLLib2 POST.

您的服务器可能正在执行从 myserver/post_service 到 myserver/post_service/ 的 302 重定向.执行 302 重定向后,请求会从 POST 更改为 GET(请参阅问题 1401).尝试将 url 更改为 myserver/post_service/.

Your server is likely performing a 302 redirect from myserver/post_service to myserver/post_service/. When the 302 redirect is performed, the request changes from POST to GET (see Issue 1401). Try changing url to myserver/post_service/.

更多推荐

使用 urllib2 进行 POST 调用而不是 GET

本文发布于:2023-10-10 22:36:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!