lundi 4 juin 2018

Uploading a csar file using REST python

I have written a test that uploads a CSAR, The problem is that I receive 415 response and not 200.

From postman it's running ok.

Postman to python code:

import requests

url = "https://x.x.x.x:port/path"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"csar\"; filename=\"csar\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--" headers = { 'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 'X-AUTH-TOKEN': "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJOQ1NPIiwiaWF0IjoxNTI4MDEyNDc0LCJleHAiOjE1MjgwOTg4NzQsImp0aSI6IjA4MTJlNDM3LTk3MDEtNDM4NS1iNGIzLTc5YzNhNWU3YTQyYyIsInVzZXJOYW1lIjoibmNzbyIsInRlbmFudCI6IlRlc3QiLCJyb2xlcyI6WyJOQ1NPQWRtaW4iXX0=.s8CuE0d4bwKQxd6jYhAM5Slx5mYmZNQCf/SGytPxzOQ=" }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

My Test: def csar_upload():

(all) imports ...

file = "/csar.csar"
upload_csar.csar_upload( file)

API Implementation:

(all) imports ...

def upload_csar(self, files):

    f = FileSystem.get_binary_file(files) 
    file_parts = {"csar": f}
    print file_parts
    response = restcall.upload_call(file_parts)

    if response == True:
        return True, response
    else:
        return False, response

Call:

(all) imports ...

class restcalls(object):

@classmethod
def upload_csar(cls, files):

    method = 'POST'
    url = system_ip() + '/path'
    headers = headers_path.someheader

    response = requests.post(method, url, headers, files=files)
    return response


I received Response: 415 Media not supported.

Headers are ok and the file is ok. Maybe the boundaries. is there any way to write it so it can work from my python test?

Thank you.

Aucun commentaire:

Enregistrer un commentaire