-1

Is there any way in a Google App Engine handler (Python - Webapp2) to modify a request parameter before redirecting it?

I am actually interested in changing the contend of a posted file (multipart_encode).

for instance:

class AnHandler(webapp2.RequestHandler):
     def post(self):
        inFile =  self.request.POST.multi['file'].file
        outFile = StringIO.StringIO()
        encrypt(inFile,outFile)
        upload_url = 'https://someurl'
        #now I need to switch inFile with outfile in self.request
        self.redirect(uri=upload_url,code=307)

feel free to propose any alternative approach but consider that the requests library is not available in GAE at now

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
  • 2
    Can you post some code of how you're doing it right now? – Jaime Gómez May 02 '15 at 17:57
  • I am not doing it but I need to do it. I have added some code – Andrea Sindico May 02 '15 at 18:15
  • 1
    Can't you create rpc request to another server/endpoint? So basically your handler is like a proxy – marcadian May 04 '15 at 05:48
  • please add an answer with some code I can try – Andrea Sindico May 04 '15 at 05:49
  • 1
    You are misunderstanding the concept of HTTP status codes. 30x codes instruct the **client** to **resend** the same query. 307 specifically asks the client to not only send the same request but also use the same request method (POST in your case). You cannot change that. What you can do is, like others recommended, take the request, manipulate the data, call whatever target URL you want to call, and then return the result to the client. – sysconfig May 04 '15 at 11:00

1 Answers1

2

So when you say redirect I'm reading that as:

You'd like to forward the modified request to another endpoint or server. You're not actually looking to redirect the user.

I'm not sure if you can do what the question is asking. When a server sends back a redirection, it's specifying a Location header which the client then goes to. You can definitely tell the client to go to another URI, but I don't think you can modify its request and tell it to use the new one.

Why not keep it simple and use a module like requests to make the modified request to the other endpoint and then return the response from that request to the client?


feel free to propose any alternative approach but consider that the requests library is not available in GAE at now

You should comment on my answer instead of or in addition to modifying your question. I don't get a notification when your question is edited.

Having said that, feel free to use urllib or urllib3 in place of requests. The idea is still the same, just a different library.

citruspi
  • 6,709
  • 4
  • 27
  • 43
  • your answer looks like a question. The statement self.redirect(uri=upload_url,code=307) works perfectly. If you are proposing a different way of changing the request data and perform redirection please add some code and example. – Andrea Sindico May 04 '15 at 05:48
  • 1
    @Sindico What you're asking for is not possible with a 307 response because 307 is an instruction to the client to repeat the *same* request to a different url, thus the content is the same. To repeat what has been said: You cannot *redirect* with different content. What you want is to *proxy* the request. So need to receive the whole request, forward it from appengine and relay the result to the client. citruspi is spot on and since your approach is wrong you would have to ask a different / the right question to get a correct code example. 42. – konqi May 04 '15 at 09:30
  • 1
    @Sindico It's not a question. I'm telling you that it __is not__ possible and asking you if another solution which I've proposed will work. I can't tell you that it will work because I don't know what your application is, what it's supposed to do, how it's supposed to work, etc. `self.redirect(uri=upload_url,code=307)` absolutely works, but it doesn't do what you're looking for. And regarding `please add some code and example`, I've given you a solution, I'm not going to implement it for you. – citruspi May 04 '15 at 15:47