0
votes

I am trying to call a post request to upload a file to Archer. Please find my code below :

    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    data = open('testdoc.txt','rb').read()

    url = "http://rsaarcher/platformapi/core/content/attachment" #my archer url

    token = "<my session id token>"
    headers = {
       'Accept':'application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Authorization': 'Archer session-id="'+token+'"',
       'content-type': "application/json;",
       'cache-control': "no-cache",
    }
   response = requests.request("POST", url, data=data, headers=headers, verify = False)

   print(response.content)
   print(response.status_code)

I am getting the following error : b'{"Message":"The request contains an entity body but no Content-Type header. The inferred media type \'application/octet-stream\' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type \'AttachmentWrapper\' from content with media type \'application/octet-stream\'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\\r\\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}' Status code : 415

3

3 Answers

0
votes

Nakshatra, did you convert the file you're uploading to base64 beforehand?

0
votes

Ahh, I realized the data you're sending is the incorrect formatted. It should be as follows:

{
    "AttachmentName" : "myFile.docx",
    "AttachmentBytes" : "[base64 here]"
}
0
votes
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

with open("1.png", "rb") as img_file:
    data = base64.b64encode(img_file.read())

url = "http://rsaarcher/platformapi/core/content/attachment" #my archer url
token = "<my session id token>"
headers = { 'Accept': 
'application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Authorization': 'Archer session-id="'+token+'"',
'content-type': "application/json;",
'cache-control': "no-cache",
}
BODY ={           
        "AttachmentName":"1.png",          
        "AttachmentBytes":data,                                   
        "IsSensitive":"true"                                                          
      }

response = requests.request("POST", url, data=BODY, headers=headers, verify = False)

I have also tried below :

    BODY = '"Attachment Bodie" {                                      
                  "AttachmentName": "testdoc.txt", 
                  "AttachmentBytes":"'+str(data)+'",             
                  "IsSensitive":"true"                                               
                                }'

But getting the same error