function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DJ 367DJ 367 

upload attachment from attachment to external server thru REST api.

Hello All,
I want to send attachmnet of attachment object to an external server thru REST api, Can some one please help me with code. Thanks
Raj VakatiRaj Vakati
You can able to use the below  Rest API ..


 
Rest URL      : <Instance URL>services/data/v23.0/sobjects/Document/
Method               : HTTP POST

Refer this link 

https://salesforce.stackexchange.com/questions/24671/upload-files-to-a-case-in-salesforce-via-rest-api
https://developer.salesforce.com/forums/?id=9060G000000XdhZQAS
https://medium.com/@weirdthinker15/uploading-a-document-to-salesforce-using-rest-api-a7481324812a
https://docparser.com/blog/post-file-salesforce-apex-external-http-webservices/
http://www.wadewegner.com/2014/05/uploading-an-attachment-to-salesforce-with-the-soap-and-rest-apis/
https://community.jivesoftware.com/thread/295296
Neha AggrawalNeha Aggrawal
Hi,

If you are still looking for a solution, this is my code in PHP which uploads the attachment to Salesforce Documents.
 
<?php
//Add the below credentials to a separate file and call here using include
/***************oAuth2 Connected App Credentials************************************/
define("CLIENT_ID", "<Replace with your Client Id");
define("CLIENT_SECRET", "Replace with your Client Secret");
define("REDIRECT_URI", "Replace with Redirect URI");
define("LOGIN_URI", "Replace with your Login URI");
define("USER_NAME", "Your Username to login to Salesforce");
define("PASSWORD", "Your Password to login to Salesforce");
define("SECURITY_TOKEN", "Your security token");

/***************oAuth2 Connected App Credentials************************************/

$token_url = LOGIN_URI . "/services/oauth2/token";
//Login to Salesforce
$params =  "&grant_type=password"
    . "&client_id=" . CLIENT_ID
    . "&client_secret=" . CLIENT_SECRET
    . "&username=".USER_NAME
	."&password=".PASSWORD
	."&security_token=".SECURITY_TOKEN;

$curl = curl_init($token_url);

//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
	$errstr="Error: call to token URL $token_url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl).'<br>';
    //die("Error: call to token URL $token_url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

curl_close($curl);

$response = json_decode($json_response, true);

$access_token = $response['access_token'];
$instance_url = $response['instance_url'];

if (!isset($access_token) || $access_token == "") {
	$errstr=$errstr." Error - access token missing from response!".'<br>';
   // die("Error - access token missing from response!");
}

if (!isset($instance_url) || $instance_url == "") {
	$errstr=$errstr." Error - instance URL missing from response!".'<br>';
   // die("Error - instance URL missing from response!");
}
//Get the user uploaded file
$file = $_FILES["file"]["tmp_name"];
$remote_file = $_FILES["file"]["name"];

$post_text=[
    "Description" => "Example File",
    "Keywords" =>  "Rest API File Upload",
    "FolderId" => "Replace with your Folder Id",
    "Name" => "Example Name",
    "Type" => "pdf",
	"body" => base64_encode(file_get_contents($file)),
];
$json_test=json_encode($post_text);
//Upload the file to Documents

$url=$instance_url."/services/data/v45.0/sobjects/Document/";
 $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($curl, CURLOPT_HTTPHEADER,array("Authorization: OAuth $access_token","Content-type: application/json;boundary:a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq"));
curl_setopt($curl, CURLOPT_POST, true);				   
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json_test);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLINFO_HEADER_OUT, true);
 $json_response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ( $status != 201 ) {
        error_log("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
		//echo "error_message";
		exit;
    }
?>


This is my blog post with complete explanation: https://initaura.com/uploading-attachment-through-rest-api-salesforce-simple-working-solution/

Thanks and Regards, 
Neha Aggrawal
www.initaura.com - Everything Salesforce (https://www.initaura.com)