2

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi thank you for your response. The problem now is that the POST url which is localhost:8000 is not replaced when requests.post is called. Since the form is posted to localhost:8000/upload initially to reach the upload method in views the POST url is not correct. I would expect that requests would replace it but it doesn't
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.