Web服务不起作用

时间:2018-08-16 03:36:09

标签: rest file-upload jersey-client

当我尝试使用Java来创建REST api时,遇到了无效的内容类型错误。但是同一件事是Python版本的代码中的工作文件。

我正在做的是,读取CSV文件并将其放在URL中并调用它。

这两个代码段都是

Python

user = 'AnaplanCertificate ' + str(base64.b64encode((
   f'{username}:{cert}').encode('utf-8')).decode('utf-8'))

url = (f'https://api.anaplan.com/1/3/workspaces/wokid/models/modid/'+f'files/{fileData["id"]}')

putHeaders = {
'Authorization': user,

'Content-Type': 'application/octet-stream'
}

dataFile = open(fileData['name'], 'r').read().encode('utf-8')

fileUpload = requests.put(url,
                      headers=putHeaders,
                      data=(dataFile))

if fileUpload.ok:
    print('File Upload Successful.')
else:
    print('There was an issue with your file upload: '
      + str(fileUpload.status_code))

Java

Client client = ClientBuilder.newClient();
Entity<String> payload = Entity.text(value);
Response response  =client.target("https://api.anaplan.com/1/3/workspaces/wokid/models/modid/files/113000000011")
       .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
        .header("Authorization", user)
        .header("Content-Type", "application/csv")
        .header("encoding","UTF-8")
        .put(payload);

从Java运行时得到的错误消息是

  

Apache Tomcat / 8.0.37-错误报告H1 {font-family:Tahoma,Arial,sans-serif; color:white; background-color:#525D76; font-size:22px;} H2 {font-family:Tahoma ,Arial,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:16px;} H3 {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76; font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif; color:black; background-color:white;} B {font-family:Tahoma,Arial,sans-serif; color:white; background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif; background:white; color:black; font-size:12px;} A {color:black;} A.name {color:黑色;}。line {高度:1像素;背景颜色:#525D76; border:none;}

HTTP状态415-不支持的媒体类型

类型状态报告

消息不受支持的媒体类型

描述服务器拒绝了此请求,因为请求实体的格式不受请求的资源所请求的方法支持。

Apache Tomcat / 8.0.37

0 个答案:

没有答案