1

I am posting some parameters to a web-service as follows:

requests.post("http://**.***.**.**:****/MyLogin/services/DBConnection/callLoginProcedure?inputPhoneNumber=" + phone + "&inputPassword=" + password)

The "callLoginProcedure" returns an integer value but I couldn't manage to get that value. How can I get this return value?

views.py:

def index(request):
    post = request.POST.copy()
    if post.get('login_button'):
        phone = post.get('phone_num')
        password = post.get('password')



        requests.post("http://**.***.**.**:****/MyLogin/services/DBConnection/callLoginProcedure?                                                   
            inputPhoneNumber=" + phone + "&inputPassword=" + password)
        r = request.GET.get("return", "-1")
        # if r == 1:
        #     messages.info(request, 'successful!')
        # else:
        #     messages.info(request, 'unsuccessful!')
        messages.info(request, r)
    return render(request, 'login/index.html')

urls.py:

urlpatterns = [
url(r'^$', views.index, name = 'index'),
path(r'^$', views.index, name = 'script'),

]

Edit:

My problem is solved but I get proxy error when requests.post function is called. I don't know how to solve it. The stacktrace is as follows:

    Request Method: POST
Request URL: http://localhost:8000/login/

Django Version: 2.2.3
Python Version: 3.7.3
Installed Applications:
['login',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware']



Traceback:

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  603.                                                   chunked=chunked)

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in _make_request
  344.             self._validate_conn(conn)

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in _validate_conn
  843.             conn.connect()

File "C:\Program Files\Python37\lib\site-packages\urllib3\connection.py" in connect
  370.             ssl_context=context)

File "C:\Program Files\Python37\lib\site-packages\urllib3\util\ssl_.py" in ssl_wrap_socket
  368.     return context.wrap_socket(sock)

File "C:\Program Files\Python37\lib\ssl.py" in wrap_socket
  412.             session=session

File "C:\Program Files\Python37\lib\ssl.py" in _create
  853.                     self.do_handshake()

File "C:\Program Files\Python37\lib\ssl.py" in do_handshake
  1117.             self._sslobj.do_handshake()

During handling of the above exception ([SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)), another exception occurred:

File "C:\Program Files\Python37\lib\site-packages\requests\adapters.py" in send
  449.                     timeout=timeout

File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  641.                                         _stacktrace=sys.exc_info()[2])

File "C:\Program Files\Python37\lib\site-packages\urllib3\util\retry.py" in increment
  399.             raise MaxRetryError(_pool, url, error or ResponseError(cause))

During handling of the above exception (HTTPSConnectionPool(***): Max retries exceeded with url: **** (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)')))), another exception occurred:

File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\TOLGA\Desktop\PythonWebProjects\WebLogin\login\views.py" in index
  53.         response = requests.post('{}?{}'.format(endpoint, qd.urlencode()))

File "C:\Program Files\Python37\lib\site-packages\requests\api.py" in post
  116.     return request('post', url, data=data, json=json, **kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\api.py" in request
  60.         return session.request(method=method, url=url, **kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\sessions.py" in request
  533.         resp = self.send(prep, **send_kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\sessions.py" in send
  646.         r = adapter.send(request, **kwargs)

File "C:\Program Files\Python37\lib\site-packages\requests\adapters.py" in send
  514.                 raise SSLError(e, request=request)

Exception Type: SSLError at /login/

1 Answer 1

1

First of all, I strongly suggest that you do not encode the URL yourself, but use Django's QueryDict for example, like:

from django.http import QueryDict

qd = QueryDict(mutable=True)
qd.update(inputPhoneNumber=phone, inputPassword=password)

For example:

>>> qd = QueryDict(mutable=True)
>>> qd.update(inputPhoneNumber=phone, inputPassword=password)
>>> qd.urlencode()
'inputPhoneNumber=0015550183&inputPassword=some_password'

Next, you should read out the response of the POST request, like:

from django.http import QueryDict
from json.decoder import JSONDecodeError
import requests

endpoint = 'http://68.183.75.84:8080/Calculator/services/DBConnection/callLoginProcedure'

def index(request): post = request.POST if request.POST.get('login_button'): qd = QueryDict(mutable=True) qd.update( inputPhoneNumber=request.POST.get('phone_num'), inputPassword=request.POST.get('password') ) response = requests.post('{}?{}'.format(endpoint, qd.urlencode())) try: result = response.json() else JSONDecodeError: result = -1 # ... return render(request, 'login/index.html')
Sign up to request clarification or add additional context in comments.

11 Comments

First, thanks for your elegant answer. I have done as you said. However, now I get proxy error: "ProxyError at /login/HTTPConnectionPool" Do you have any suggestions?
@Exqra: usually that means that it can not contact the server (for one reason or another). Are you sure no firewall is blocking the request?
I've disabled firewall, but still get the same error.
@Exqra: can you make the call manually, for example with curl?
How can I use "curl"? I am new to python
|

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.