Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import requests, config, json
  2.  
  3. def SendFile ():
  4.  
  5.     #part for login
  6.  
  7.     login_url = 'https://api.us2.apiconnect.ibmcloud.com/cnx-gbl-org-quality/quality/v2/secm/oam/oauth2/token'
  8.     login_header = {
  9.         'Content-Type':'application/x-www-form-urlencoded',
  10.         'AppCode':'DCMWEBPORTAL',
  11.         'X-IBM-Client-Id': config.ibm_client_id
  12.     }
  13.     login_body_raw = 'grant_type=password&scope=security&username=' + config.username + '&password=' + config.pwd+'&client_id=' + config.ibm_client_id
  14.  
  15.     login_response = requests.post(headers = login_header, url=login_url, data=login_body_raw)
  16.     login_json = json.loads(login_response.content)
  17.     auth_token = login_json['oauth2']['access_token']
  18.     jwt_token = login_json.get('jwt', 0)
  19.     print ('jwt :' + jwt_token)
  20.     print ('auth :' + auth_token)
  21.  
  22.     #part for file upload
  23.  
  24.     file_url = 'https://api.us2.apiconnect.ibmcloud.com/cnx-gbl-org-quality/quality/v7/dm/loads/upload'
  25.     files = {
  26.         #'filename1': open('C:\\Users\\jan.svehlak\\scripts\\files_upload\\UploadFile.json', 'rb'),
  27.         'filename2': open('C:\\Users\\jan.svehlak\\scripts\\files_upload\\file_doc.doc','rb')
  28.     }
  29.     file_headers = {
  30.         'Accept': 'application/json',
  31.         'AppCode': 'DCMWebToolApp',
  32.         'Authorization': 'Bearer ' + auth_token,
  33.         'Content-Type': 'multipart/form-data',
  34.         'jwt': jwt_token,
  35.         'X-IBM-Client-Id': config.ibm_client_id
  36.     }
  37.     response = requests.post(headers = file_headers, url = file_url, files = files)
  38.     respcode = response.status_code
  39.  
  40.     if respcode == 415:
  41.         print('ERR: Unsupported media type')
  42.         print('Response code: ' + str(respcode))
  43.         print('Response headers: ' + str(response.headers))
  44.         print('Response body: ' + str(response.content))
  45.  
  46. SendFile()
  47.  
  48. ////
  49. ERR: Unsupported media type
  50. Response code: 415
  51. Response headers: {'X-Backside-Transport': 'OK OK', 'Connection': 'Keep-Alive', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Date': 'Tue, 20 Feb 2018 08:27:09 GMT', 'X-Global-Transaction-ID': '35275025', 'User-Agent': 'IBM-APIConnect/201', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache', 'Expires': '-1', 'Request-Context': 'appId=cid-v1:9cd31ff9-864d-4bd0-9382-3e213ef4877f', 'Access-Control-Expose-Headers': 'APIm-Debug-Trans-Id, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-Global-Transaction-ID', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST', 'Access-Control-Allow-Credentials': 'false', 'X-RateLimit-Limit': 'name=rate-limit,1000;', 'X-RateLimit-Remaining': 'name=rate-limit,999;', 'Content-Type': 'application/octet-stream'}
  52. Response body: b''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement