如何将 Django 请求发布到外部服务器 [英] How to post a django request to external server

查看:34
本文介绍了如何将 Django 请求发布到外部服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 django 视图中有这个方法可以将文件发布到不同的服务器.我收到一个 HTTP 415 错误,抱怨请求的媒体类型.我已经调试了请求并将其内容复制并粘贴到 fiddler 中.当我从提琴手发布相同的内容时,它起作用了.所以我不明白为什么它不能使用 python requests 包.

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?

谢谢.

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 对象传递给 requests.post.

You are passing a full django.http.HttpRequest object to requests.post, when you only need its post data.

这篇关于如何将 Django 请求发布到外部服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆