Python Forum
requests issue with post on dot_net api - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: requests issue with post on dot_net api (/thread-23915.html)



requests issue with post on dot_net api - Heinrich - Jan-22-2020

Hi,

my code is trying to upload a file and a data field to a .net API but I am getting an error (see below's output).
User enters the data and file via a web form (using django web framework).

        if 'cert_btn' in request.POST and certificate_form.is_valid():
            try:
                certificate_password = certificate_form.cleaned_data.get('certificate_password')
                print(certificate_password)
                print(type(certificate_password))
                tenants_id = certificate_form.cleaned_data.get('tenants_id')
                print(tenants_id)
                file_for_upload = request.FILES['certificate_pfx'].read()
                print(type(file_for_upload))
                print(file_for_upload)
                # files = {
                #     'file': open(file_for_upload, 'rb')
                # }

                x = bearer_token()

                request_header = {
                    # 'Content-Type': 'multipart/form-data',
                    # 'Ocp-Apim-Subscription-Key': ocp_apim_subscription_key,
                    'Authorization': 'Bearer ' + x
                }

                # payload = '{\'payload\': {\'password\': \'' + certificate_password + '\'}}'
                payload = {'payload': '{\'password\': \'' + certificate_password + '\'}'}
                # payload = {
                #     'payload': {
                #         'password': certificate_password
                #     }
                # }

                message = [
                    ('file', file_for_upload)
                ]

                service_url = str(tenantsservice_url) + 'tenant/' + str(tenants_id) + '/appSettings/ssl-certificate'
                print(service_url)
                upload_certificate_response = requests.post(service_url, headers=request_header,
                                                            data=payload, files=message)
Output:
"StatusCode":500,"Message":"StartIndex cannot be less than zero.\nParameter name: startIndex"



RE: requests issue with post on dot_net api - Larz60+ - Jan-23-2020

Please post entire error traceback as it contains line numbers and also shows execution leading up to the error.