2

I have a problem with POST request with Python/Django and Minio server, this is the code

from django.http import HttpResponse
import json
from minio import Minio

minioClient = Minio('mypath:9000',
                access_key='mykey',
                secret_key='mysecret',
                secure=False)


def getMessage(request):
   if request.method == 'POST':

       data = json.loads(request.body.decode('utf-8'))

       for obj in data['files']:
           ...do some stuff....

           minioClient.fget_object(myvar, myvar2, '/tmp/processing')

    return HttpResponse(file)

The problem is that the request won't work if I don't remove the import at the beginning and I can't understand why. This is the error generated:

HTTPConnectionPool(host='myhost', port=8001): 
Max retries exceeded with url: /myurl/ 
(Caused NewConnectionError
('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fcbeab21160>: 
Failed to establish a new connection: [Errno 111] Connection refused',))

and this is the script that make the request is this one:

.... some code....
try:
   r = requests.post("http://myurl:8001/mypath/", data=my_data, timeout=1)
except Exception as e:
   print(e)

I've already tried to increase the timeout but it's not working and of course, I've tested the Minio part in another script, the import it's generating this error only in this request script.

Thanks for the help

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
Katia Riva
  • 21
  • 2

1 Answers1

1

From docs for urllib3:

request(method, url, fields=None, headers=None, **urlopen_kw)¶ Make a request using urlopen() with the appropriate encoding of fields based on the method used.

Maybe you could try something like this:

r = http.request('POST', "http://myurl:8001/mypath/",
                 headers={'Content-Type': 'application/json'},
                 body=encoded_data)
Anatoly Strashkevich
  • 1,834
  • 4
  • 16
  • 32
  • Unfortunately it's not working with this code either :( – Katia Riva Oct 19 '16 at 06:10
  • when you post something on server you cant post just data, you need to include appropriate headers and other stuff, like cookies, csrf token, if server uses HTTPS you need to establish secure connection, it doesnt work because server suspect something wrong with your request, it looks suspicious, try different options – Anatoly Strashkevich Oct 19 '16 at 06:22
  • as a tip, may be you don't know that, if you use chrome you can go to developer tools->on top will be network button, on the left will be all your requests, you can see what chrome uses when he go to your web page, and you can copy this info – Anatoly Strashkevich Oct 19 '16 at 06:36