简体   繁体   English

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

[英]Salesforce - SharePoint File Upload 400 Bad Request Error

I am trying to upload file from Apex to Sharepoint but getting error as '400 Bad Request'.But work from JS CODE.我正在尝试将文件从 Apex 上传到 Sharepoint,但收到“400 错误请求”的错误。但是可以通过 JS CODE 工作。 Following is my code snippet :以下是我的代码片段:

  1. Apex Code顶点代码
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. JS CODE代码
var myHeaders = new Headers();
        myHeaders.append("Authorization", "Bearer " + Token);
        myHeaders.append("Content-Type", "application/json;odata=verbose");

        var requestOptions = {
          method: 'POST',
          headers: myHeaders,
          body: fileBuffer,
        };

        fetch('https://sample.sharepoint.com/sites/siteName/_api/web/GetFolderByServerRelativeUrl(\'/sites/siteName/Shared Documents\')/Files/Add(url=\'test.txt\', overwrite=true)', requestOptions)
          .then(response => response.text())
          .then(result => console.log(result))
          .catch(error => alert('error', error));
      } 

Thankyou谢谢

As per the documentation salesforce can't communicate to FTP directly that means can't Read/Write file at FTP Server.根据文档, salesforce 无法直接与 FTP 通信,这意味着无法在 FTP 服务器上读取/写入文件。

I also faced this issue and this is how I resolved it:-我也遇到了这个问题,这就是我解决它的方法:-

Step1: Create & host an External API on in any language(C#, Python) that takes two parameters one as fileName and the other one as fileData and uploads that file.第 1 步:使用任何语言(C#、Python)创建和托管外部 API,该 API 将两个参数作为fileName ,另一个作为fileData并上传该文件。

Step2: At Salesforce end, consume that API using HttpRequest and pass your file as filedata and fileName .步骤 2:在 Salesforce 端,使用HttpRequest使用该 API 并将您的文件作为filedatafileName传递。

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM