+ 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 

Neha AggrawalNeha Aggrawal

If you are still looking for a solution, this is my code in PHP which uploads the attachment to Salesforce Documents.
//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

$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));


$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"];

    "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)),
//Upload the file to Documents

 $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";

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)