Post and redirect to cross domain URL - python

I want to post some data (say id=123) to a cross domain URL and then redirect to that URL. Code:
#app.route("/postreq", methods=['GET','POST'])
def my_webservice():
return redirect('127.0.0.1:3005/developer?id=123')
This redirect works fine but I want to send id via post request to hide it from query string. Any suggestions?

First of all, redirecting GET to POST should be avoided, as the two verbs have different meanings: GET requests should be idempotent, POST request are supposed to modify the internal state of the application.
Secondly, after a POST, browsers usually can be redirected to a resource that they will fetch with GET (303 redirect code), or using the same POST verb (307 redirect code), but the spec (https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) does not show a way to do GET -> POST.

Related

Return custom http code after redirection in flask

there I am new to the flask.
The scenario:
I am trying to redirect to a certain route after submitting the form.
So I am using flask redirect for this along with code parameter.
#topics_bp.route('/create_topic/',methods =['GET','POST'] )
def create_topic():
if request.method == 'GET':
#send the form for create topic!
formData = TopicForm()
return render_template('create-topic.html',form = formData)
if request.method == 'POST':
# check the post method and redirect
return redirect(url_for('topic.topics'),code=201)
Basically , I want to return HTTP code 201 for a record created and then redirect to the intended route.
But if I do like this, it simply returns a redirection page. Every time I need to click manually.
Is there any workaround to return 201 code and redirect automatically?
Thanks in advance!
I want to return HTTP code 201 for a record created and then redirect to the intended route.
That's not something you can do with HTTP. A redirect is itself a specific HTTP 30x status code:
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3, and a Location header holding the URL to redirect to.
Either you return a 201 status code or you return one of the HTTP redirection status codes. You can't do both.
The flask.redirect() function generates a valid 30x response (with the required Location header), and the documentation for the function states what redirect status codes are supported:
Supported codes are 301, 302, 303, 305, 307, and 308. 300 is not supported because it’s not a real redirect and 304 because it’s the answer for a request with a request with defined If-Modified-Since headers.
The function doesn't enforce this, however; the status code is not validated.
You need to distinguish between a browser and other clients here. A 201 Created response is something you typically return from a REST API to a client that expects simple JSON or XML interactions at a programmable level. Redirects, on the other hand, are more typically used in a frontend, in the presentation layer.
If you are coding a HTML-based front-end, just return a redirect. Humans don't read response codes, they read rendered HTML pages. They don't care and don't need to know the exact HTTP codes used to make the browser do the right thing. Given that your route also includes a form, you are almost certainly building a site for humans and not for programmatic clients.
If you are building a REST API, then return a 201 response, and document that the API client will have to make a new request based on the Location header you included, or on something in the body of the response. A HTML browser will not follow the Location header on 201 responses however.
I then would not use the redirect() function for this, even if it does allow you to use 201 as the status code, because it always produces a (simple) HTML body containing the text you see in your browser about an automatic redirect.

How do i log-in with a POST request in a Django view?

On a certain page of my Django project, i'm trying to add a feature where, when a page is loaded, a request is sent to an external Python script, this Python script will retrieve some data and send it as a response to the Django view, which should show it on the HTML using Jquery. To send the requests, i'm using the Request module.
I'm having some problems sending the response with the Json data to the Django view, here is what i tried:
Here is how the request is sent:
import requests
req = requests.post('http://127.0.0.1:8000/myview', json={"test": "testjson"})
print('SENT')
What i tried in the view:
def myTestView(request):
if request.method == 'POST':
received_json_data=json.loads(request.body)
print(received_json_data)
print('Received request')
I'm trying to print the response in my console just for debugging purposes. The problem is that i don't see the request being printed in the console. Instead, i only see this:
[24/Dec/2019 12:00:40] "POST /tv/ HTTP/1.1" 302 0
[24/Dec/2019 12:00:40] "GET /account/login/?next=/tv/ HTTP/1.1" 200 23357
Which means that i have to add my credentials, since the view is #login_required. The problem is that this request is fired when the user loads the page or performs a certain operation, and the request will hold sensitive data. This task, however, can be performed by any user on the site, so i can't add the credentials of only one in my external request. How can i fix this? Any advice is appreciated

How to set a cookie on a redirect in python

I am writing a Proxy Server where I will intercept a request, and check the header to see if it has a cookie named URID, and if it does not, I want to redirect the request to a fw.asta.com, see if this domain has a cookie called URID, and if it doesn't, I want to set the cookie for fw.cisco.com, and then redirect to the originally intended site, and then set the same cookie for that site as well. If the cookie exists for the fw.asta.com site, I will use that cookie to redirect to the originally intended site, and if the originally intended site has the URID cookie, I will simply serve the request on behalf of the site.
So I have been struggling with this for quite some time. I have a feeling like I don't understand what actually happens when I set a cookie during a redirection.
I have this code right here:
def do_GET(self):
#seperate the path into parts
(scm, netloc, path, params, query, fragment) = urlparse.urlparse(self.path, 'http')
#check the header for the urid cookie, returns the URID if present
urid = self.find_urid()
if netloc == 'fw.asta.com':
if urid:
#redirect to the originally intended site and set the cookie
path = self.path[23:]
self.redirect_with_urid(path, urid)
else:
#redirect to fw.asta.com
urid = 333
self.redirect_with_urid(self.path, urid)
else:
if urid:
#simply serve the site because if has the URID
self.final_destination()
else:
#no urid, so we need to go the fw.asta.com to check if the urid is there
self.redirect('http://fw.asta.com/?url=' + str(self.path))
As you can see the logic makes sense, however when I run the code, I run into an infinite redirect loop to the originally intended site.
When I get the redirect response, I can clearly see that the set-cookie field is there, however, when I get the redirected request, it does not send the cookie back to me.
Is this even the right way to go about the problem? Any advice would be appreciated.

how to listen to post requests from foursquare push api

I have set up everything required for the foursquare push api, including a secure server.
Now my question is what do I need to do to get data from that request and display on my web page. Foursquare API sends a POST to a URL which I can handle with a view.
Do I need to use AJAX calls, or just handle the post request in a view and update something in the database and show data from that database with ajax.. open to suggestions.
No, all you need to do is send requests and receive responses. Please take a look at this -> python-requests.org -> The documentation provided how you can send and receive requests.
So, for example, you could send a POST request like so:
r = requests.get('https://api.github.com/user', <your arguments here>)
And now r would contain the POST response from the said url.

Get POST data from WebKitNetworkRequest

I am trying to send data from a javascript app running in GTK webkit to Python via a HTTP request with the data sent in POST.
I can capture the request using resource-request-starting and checking the uri of the request.
I know the request works because I can send data through the request headers and view it with
def on_resource_request_starting(view, frame, resource, request, response):
uri = urllib.unquote(request.props.uri)
if uri.startswith('http://appname.local/'):
print request.get_message().request_headers.get_one('foobar')
But when I use print request.get_message().request_body.data I don't get anything.
How do I view the POST data?
I haven't used this API, but I believe you need to call the binding's equivalent of soup_message_body_flatten() before reading the data field. See the documentation for SoupMessageBody in the C API.
So, at a guess:
print request.get_message().request_body.flatten().data
Hooking to SoupSession "request-queued" signal and getting
buffer(s) using soup_message_body_get_chunk(soupmsgbody, num);
seems to work (in webkitgtk1 today, Jun 2015).
webkit_get_default_session() returns the SoupSession in question.

Categories