Salesforce-SharePoint文件上传400错误的请求错误

时间:2020-08-28 15:15:48

标签: javascript sharepoint salesforce apex apex-code

我正在尝试将文件从Apex上传到Sharepoint,但由于出现“ 400 Bad Request”而出错,但是可以从JS CODE进行工作。以下是我的代码段:

  1. 顶点代码
[ 69, 2, 221, 8974 ]
Sum: 9266
[ 7, 2, 221, 8974 ]
Sum: 9204
[ 7, 69, 221, 8974 ]
Sum: 9271
[ 7, 69, 2, 8974 ]
Sum: 9052
[ 7, 69, 2, 221 ]
Sum: 299
Min: 299
Max: 9271
[ 210437958, 673982045, 375809214, 380564127 ]
Sum: 1640793344
[ 769082435, 673982045, 375809214, 380564127 ]
Sum: 2199437821
[ 769082435, 210437958, 375809214, 380564127 ]
Sum: 1735893734
[ 769082435, 210437958, 673982045, 380564127 ]
Sum: 2034066565
[ 769082435, 210437958, 673982045, 375809214 ]
Sum: 2029311652
Min: 1640793344
Max: 2199437821
[]
Sum: 0
[]
Sum: 0
[]
Sum: 0
[]
Sum: 0
[]
Sum: 0
Min: 0
Max: 0
  1. JS代码
Http http = new Http();
HttpRequest httpRequestToSend = new HttpRequest();  
httpRequestToSend.setEndpoint('https://sample.sharepoint.com/sites/siteName/_api/web/GetFolderByServerRelativeUrl(\''+'/sites/siteName/Shared Documents'+'\')/Files/Add(url=\''+'document3.txt'+'\', overwrite=true)');
httpRequestToSend.setMethod('POST');
httpRequestToSend.setHeader('Authorization', 'Bearer ' + token);
httpRequestToSend.setHeader('Content-Type','application/json; odata=verbose');

httpRequestToSend.setBodyAsBlob(Blob.ValueOf('test Message'));
System.debug('***** httpRequestToSend-->' + httpRequestToSend);
Http http1 = new Http();   
HttpResponse httpResponse1 = http1.send(httpRequestToSend);  
System.debug('***** httpResponse-->' + httpResponse1.toString());
    System.debug(httpResponse1.getBody());

谢谢

1 个答案:

答案 0 :(得分:0)

根据文档 salesforce无法直接与FTP通信,这意味着无法在FTP服务器上读取/写入文件。

我也遇到了这个问题,这就是我解决的方法:-

第一步:使用任何一种语言(C#,Python)创建并托管一个外部API,该语言具有两个参数,一个参数为 fileName ,另一个参数为 fileData 并上传该参数文件。

步骤2:在Salesforce端,使用 HttpRequest 使用该API,并将文件作为 filedata fileName 传递。

public void uploadFileToFTP_Service(string fileName,string fileData)
{
    string value='{"fileName":"'+fileName+'","fileData": "'+fileData+'"}';
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://yourhostedApIPath:9001/data');
    req.setMethod('POST');
    req.setTimeout(120000);        
    req.setHeader('content-type','application/json; charset=utf-8');
    req.setBody(value);        
    Http http = new Http();
    HttpResponse res = http.send(req);
    system.debug('Status code: ' + res.getStatusCode());
}