添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Welcome to Support!

Search for an answer or ask a question of the zone or Customer Support.

Need help? Dismiss Show All Questions sorted by Date Posted

Show

sorted by

Faiza Iqbal Faiza Iqbal

POST a binary data file to 3rd party application

Hi, I am new to the REST API in Salesforce. I have to POST a binary csv file to the 3rd party application and the given below is curl example: curl --request POST \ --header 'Authorization: Bearer <token> \ --header 'Content-type: text/csv' \ --header 'Accept: text/plain' \ --data-binary @/myhome/salesdata-2018-03-26.csv \ https://admin.testsite.com/hapi/merchant/<id>/sales-data/api And here is my apex code: String fileName = 'salesdata-' +date.today().Day() +'-'+ date.today().Month() +'-'+ date.today().Year()+'.csv'; String targetURL = 'https://entufl5t6i91i.x.pipedream.net'; // assemble the body payload String header = 'Content-Disposition: form-data; name="file"; filename="' + fileName + '"; Content-Type: application/octet-stream; '; String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\n\n')); String bodyEncoded = EncodingUtil.base64Encode(csvBlob); Blob bodyPayload = EncodingUtil.base64Decode(headerEncoded+bodyEncoded); // send out the request HttpRequest req = new HttpRequest(); req.setBodyAsBlob(bodyPayload); req.setHeader('Authorization', 'bearer hjghj'); req.setHeader('Content-Type', 'text/csv'); req.setMethod('POST'); req.setEndpoint(targetURL); req.setTimeout(60000); Http http = new Http(); HttpResponse response = http.send(req); system.debug('response: ' + response); catch(Exception e) system.debug('e:' + e);


And the request bin URL is https://requestbin.com/r/entufl5t6i91i/1d4zZyJdIarNLef7Gq5SlveeRih
And it does not look right.

When I POST it to actual 3rd part application the response is 400 and the message is 'bad request'. Kindly point out what I am doing wrong here.

Many thanks!

Shirisha Shirisha (Salesforce Developers)
Hi Faiza,

Greetings!

Status code 400 is for the bad request which means the data that you are passing is not mapped properly.

Can you please check the blog ( https://docparser.com/blog/post-file-salesforce-apex-external-http-webservices/ ) which will guide you to post the file using Apex to external web services.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri