0

我正在尝试调用发布请求以将文件上传到 Archer。请在下面找到我的代码:

    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)

我收到以下错误:b'{"Message":"请求包含实体主体但没有 Content-Type 标头。此资源不支持推断的媒体类型 \'application/octet-stream\'。", "ExceptionMessage":"没有 MediaTypeFormatter 可用于从媒体类型为 \'application/octet-stream\' 的内容中读取类型为 \'AttachmentWrapper\' 的对象。","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException" “StackTrace”:“在 System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent 内容,类型类型,IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\\r\\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable1 格式化程序,IFormatterLogger 格式化程序记录器,CancellationToken 取消令牌)”}'状态码:415

4

3 回答 3

0

啊,我意识到您发送的数据格式不正确。它应该如下:

{
    "AttachmentName" : "myFile.docx",
    "AttachmentBytes" : "[base64 here]"
}
于 2020-01-29T14:27:26.243 回答
0
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)

我也在下面尝试过:

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

但得到同样的错误

于 2020-01-30T06:01:35.607 回答
0

Nakshatra,您是否事先将要上传的文件转换为base64?

于 2020-01-28T13:46:39.483 回答