如何向外部服务器发送django请求 [英] How to post a django request to external server
问题描述
所以我在django视图中有这个方法将文件发布到不同的服务器。我收到一个HTTP 415错误,抱怨请求的媒体类型。我已经调试了请求,并将其内容复制并粘贴到提琴手中。当我从提琴手那里发贴的时候,它起作用。所以我不明白为什么它不能使用python请求包。
Hi so I have this method in django views to post the file to a different server. I get an HTTP 415 error complaining about the media type of the request. I have debugged the request and copied and pasted its contents in fiddler. When I posted the same from fiddler it worked. So I don't understand why it does not work using python requests package.
任何人都可以帮助我吗?
Can anyone help me with this?
感谢。
Thanks.
def upload(request):
if request.method == 'POST':
url=settings.WEBSERVICES_URL+'validate'
r = requests.post('http://localhost:9090/validate',data=request)
r2 = requests.get('http://localhost:9090/test')
return render_to_response("upload.html", context_instance=RequestContext(request))
else:
return render_to_response("upload.html", context_instance=RequestContext(request))
推荐答案
执行此操作:
r = requests.post('http://localhost:9090/validate', data=request.POST)
您正在将一个完整的 django.http.HttpRequest
对象传递给code> requests.post ,当您只需要其发布数据时。
You are passing a full django.http.HttpRequest
object to requests.post
, when you only need its post data.
这篇关于如何向外部服务器发送django请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!