Nathan Drake
Nathan Drake

Reputation: 257

How to post a django request to external server

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))

Upvotes: 2

Views: 9928

Answers (2)

neko_ua
neko_ua

Reputation: 470

Do this:

r = requests.post('http://localhost:9090/validate', data=request.POST)

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

Upvotes: 4

Bernhard Vallant
Bernhard Vallant

Reputation: 50786

If you look at the documentation of requests it says about the data keyword:

data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.

Django's request object is an instance of HttpRequest.You should try to put the necessary data in a dictionary and pass it to post().

Upvotes: 0

Related Questions