HttpResponse post

844 views
Skip to first unread message

Mike

unread,
Feb 16, 2009, 10:37:51 AM2/16/09
to Django users
Hi all,

I m not familiar with django and I have found several problems trying
to sent a httpresponse with post parameters. I would like to return an
httpresponse (or httpredirect) to a new url with the request
parameters via post.

Could anybody help me?
Thank you in advance,
Miguel

I have defined a new view:

def prueba(request, seccion):
if request.POST:
new_data = request.POST.copy()

[get all the parameters]
[send a mail if correct]


response = HttpResponse ("newUrl")
#¿POST?
return HttpResponseRedirect(response)

Antoni Aloy

unread,
Feb 16, 2009, 11:18:08 AM2/16/09
to django...@googlegroups.com
2009/2/16 Mike <migu...@gmail.com>:

After a post you should use a HttpResponseRedirect(url). If you need
to have access to the post parameters an option is to save them in a
session, or encode them in the url itslef.

Hope it helps!

--
Antoni Aloy López
Blog: http://trespams.com
Site: http://apsl.net

Miguel

unread,
Feb 17, 2009, 3:04:44 AM2/17/09
to django...@googlegroups.com
The problem is that I need to redirect the request to an url and post to this url the parameters I got from the initial request and display the post url. Here is my views.py:


def prueba(request, seccion):

    if request.POST:
        new_data = request.POST.copy()

        Ds_Merchant_Titular = new_data['Ds_Merchant_Titular'
]
        Ds_Merchant_MerchantCode = new_data['Ds_Merchant_MerchantCode']
        Ds_Merchant_Amount = new_data['Ds_Merchant_Amount']
        Ds_Merchant_Currency = new_data['Ds_Merchant_Currency']
        Ds_Merchant_Order = new_data['Ds_Merchant_Order']


        correo_from = Ds_Merchant_Titular
        correo_asunto = "[contacto] " + Ds_Merchant_MerchantCode
        correo_to = []

       correo_to.append(MIEMAIL)
        correo_content = Ds_Merchant_Order

        sendMail(correo_to, correo_asunto, correo_content, True, correo_from)
        params = urllib.urlencode({"Ds_Merchant_Titular": Ds_Merchant_Titular, "Ds_Merchant_MerchantCode": Ds_Merchant_MerchantCode})
        #f= urllib2.urlopen("https://sis.sermepa.es/sis/realizarPago",params)

   return f  -> it doesnt workd

   # return HttpResponseRedirect("https://sis.sermepa.es/sis/realizarPago",params)    --> it doesn't work

Karen Tracey

unread,
Feb 17, 2009, 10:15:21 AM2/17/09
to django...@googlegroups.com
On Tue, Feb 17, 2009 at 3:04 AM, Miguel <migu...@gmail.com> wrote:
[snip]

        params = urllib.urlencode({"Ds_Merchant_Titular": Ds_Merchant_Titular, "Ds_Merchant_MerchantCode": Ds_Merchant_MerchantCode})
        #f= urllib2.urlopen("https://sis.sermepa.es/sis/realizarPago",params)

   return f  -> it doesnt workd

   # return HttpResponseRedirect("https://sis.sermepa.es/sis/realizarPago",params)    --> it doesn't work

No, it wouldn't.  HttpResponseRedirect is documented (http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect) to take a single argument.  You can yourself combine params and location into the url argument to HttpResponseRedirect:

return HttpResponseRedirect("?".join(("https://sis.sermepa.es/sis/realizarPago",params)))

but that won't force the client browser to use a POST when retrieving the redirect location.  The redirect response contains a status code and location, there is no field for the server to specify what method should be used to access the specified location.  Standards actually specify that the browser must use the same method as was used on the original request (which seems to be what you want), but that is not, in fact, what most browsers do.  See:

http://en.wikipedia.org/wiki/HTTP_302

Karen

Miguel

unread,
Feb 17, 2009, 12:52:01 PM2/17/09
to django...@googlegroups.com
thanks karen, that is what I meant.
So there is no way to return in my views.py an external web html with post parameters ... it must be a way to do that, dont think so?

Karen, if I try your example
return HttpResponseRedirect("?".join(("https://sis.sermepa.es/sis/realizarPago",params))), the web site is not shown in the web browser I can see the url of my django place but not https://sis.sermepa.es/sis/realizarPago.

Antoni Aloy

unread,
Feb 17, 2009, 1:28:10 PM2/17/09
to django...@googlegroups.com
2009/2/17 Miguel <migu...@gmail.com>:

> thanks karen, that is what I meant.
> So there is no way to return in my views.py an external web html with post
> parameters ... it must be a way to do that, dont think so?
>
Yes, but you can't do it this way.

Just check for httplib and httplib2.

http://code.activestate.com/recipes/146306/

or from Python documentation in http://docs.python.org/library/httplib.html

>>> import httplib, urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
... "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
>>> conn.request("POST", "/cgi-bin/query", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
200 OK
>>> data = response.read()
>>> conn.close()

Miguel

unread,
Feb 18, 2009, 9:07:07 AM2/18/09
to django...@googlegroups.com
well, I am not a python programmer and maybe I am doing silly things or this is not as complex as I see but I not able to do what you mention above.
 I get the error,

'NoneType' object has no attribute 'read'

 

I have tried to follow http://code.activestate.com/recipes/146306/, with only fields (the fields of my form) but I get stack here  return h.file.read()  with the mentioned error.

I have also tried httplib.HTTPS but there is no way to get the results  I wanted.
(i have also commeted  errcode, errmsg, header = ..., because i got some errors)
here is my code:
 
    fields = [('Ds_Merchant_Titular', Ds_Merchant_Titular),( ......... ]      

   
    content_type, body = encode_multipart_formdata(fields)
    h = httplib.HTTPS("sis.sermepa.es",443)
    h.putrequest('POST', '/sis/realizarPago')
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.endheaders()
    h.send(body)
    #errcode, errmsg, headers = h.getreply()
    return h.file.read()




I have also tried with this code:
      
         ......
        data = urlencode(request.POST)
        resp, content = conn.request(u"https://sis.sermepa.es/sis/realizarPago", 'POST', data)
        sys.stderr.write("\n\n\n\n mikimiki ----- " + content);
    return HttpResponse(content)


But here I get the URL of my django place in my browser instead of https://sis.sermepa.es/sis/realizarPago. I mean, all the images and resources are refered to relative paths and I not able to display the page.  For sure    return HttpResponse(content) is not the way as Antoni commented.

could anybody clarify me anything?

Devin

unread,
Feb 20, 2009, 10:39:03 PM2/20/09
to Django users
I have no problem pulling pages using httplib if the pages do not
require authentication. But now I want to pull pages only after
authenticating. Access is enforced by the contributed auth package.

import httplib, urllib
params = urllib.urlencode({'this_is_the_login_form':1,'username':
'myuser', 'password': 'test'})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection("myserver")
conn.request("POST", "/login/", params, headers)
response = conn.getresponse()
print response.status, response.reason

As I said, when hitting pages that do not require auth, I am
successful and get a 200 response code. But when auth is needed, I
get 302 for the code and "found" for the status.

Any insights?





On Feb 17, 12:28 pm, Antoni Aloy <antoni.a...@gmail.com> wrote:
> 2009/2/17 Miguel <migue...@gmail.com>:> thanks karen, that is what I meant.
>
> >>> importhttplib, urllib
> >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
> >>> headers = {"Content-type": "application/x-www-form-urlencoded",
>
> ...            "Accept": "text/plain"}
>
> >>> conn =httplib.HTTPConnection("musi-cal.mojam.com:80")

Malcolm Tredinnick

unread,
Feb 20, 2009, 10:58:08 PM2/20/09
to django...@googlegroups.com
On Fri, 2009-02-20 at 19:39 -0800, Devin wrote:
> I have no problem pulling pages using httplib if the pages do not
> require authentication. But now I want to pull pages only after
> authenticating. Access is enforced by the contributed auth package.
>
> import httplib, urllib
> params = urllib.urlencode({'this_is_the_login_form':1,'username':
> 'myuser', 'password': 'test'})
> headers = {"Content-type": "application/x-www-form-urlencoded",
> "Accept": "text/plain"}
> conn = httplib.HTTPConnection("myserver")
> conn.request("POST", "/login/", params, headers)
> response = conn.getresponse()
> print response.status, response.reason
>
> As I said, when hitting pages that do not require auth, I am
> successful and get a 200 response code. But when auth is needed, I
> get 302 for the code and "found" for the status.
>
> Any insights?

So you need to act like a browser would. Submit the necessary
authentication form variables, save the returned cookie and send it on
subsequent requests.

Not sure how this is related ot the original question in the thread,
though, which was about redirecting what the user's browser retrieves,
not retrieving web pages inside Python scripts.

Regards,
Malcolm


hoamon

unread,
Mar 4, 2009, 5:11:03 AM3/4/09
to Django users
Hi all,

I write a experimental example to reach this requirement. By AJAX
method, we can make a post type form after server answer.

my example is below here:

http://hoamon.blogspot.com/2009/03/return-httpresponseredirect-by-post.html
Reply all
Reply to author
Forward
0 new messages