Question

I have an application where a device sends data to my application and the application stores the data.
My Question is What is the best way to send data from the device to the server ?
The Device can be a Smart Phone.
right now I am sending a URL request along with some data like this:

www.example.com:8888/Data/?id=12345&name=john&DOB=25-09-1974

I fetch data with GET Request in Django and then save the data. Is this Right to do ? or should I follow some other process ?

P.S The data is not Sensitive. I mean Security is not a concern.

Was it helpful?

Solution

You shouldn't upload Data to the server via "GET". That is what the "POST" method is for.

According to the HTTP/1.1 Spec:

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line

In other words, POST is used to create.

It doesn't matter if you think your data is sensitive or not. By implementing a way to alter data on the server, you are automatically offering a way that can be misused by anyone. Hence, all such interfaces have to be treated with the same thoroughness in terms of security.

Your question doesn't provide anything about what you want to to with the transferred data. Do you want to store it persistently in a database or is the data only relevant for the user during his current visit?

Basically you are offered three ways:

1. persistent storage.

You will store the data in your database. In this case you should use a form with the aforementioned "post" method.

#your_template.html
<form method="post" action="<your action">
<!-- do something -->
{{ form }}
</form>

#your_view.py
def your_view(request):
    if request.method == 'POST':        
        form = YourForm(request.POST) 
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = YourForm() # An unbound form

    return render(request, 'your_template.html', {
        'form': form,
    })

See https://docs.djangoproject.com/en/dev/topics/forms/ for more information about django forms

2. temporary storage.

you are not required to store any of these results persistently, so you can use the session mechanism of django, which will allow you to pass around data which will be just valid while a specific user is visiting your page. You can adjust the time to live of session objects

See https://docs.djangoproject.com/en/dev/topics/http/sessions/ for more information about django sessions

3. client based storage. Go all the way to the client and use cookies which will be stored on the clients machine and are just read by the server. But you have to be extra thorough while evaluating cookie values.

See Django Cookies, how can I set them? for a little cookie helper for django and take it from there

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top