Add file to Notes via REST API using NodeJS

I can successfully create the note.

I now need to upload the file and then link to the note.

Which I think I can do with this endpoint:

"/rest/v11_20/Notes/5f95bbe8-cd49-11ee-b015-fa163ec72422/file/filename"

I'm POSTing a file using FormData from my app and handling this on my NodeJS server.

Here I use axios to call the Endpoint above.

However, whatever format I pass the file in, I get either a 400 or a 422 returned from the Sugar API.

I've tried several things:

  • setting. removing / changing content-type 
  • setting / removing content disposition
  •  pass file as raw File, FormData, Buffer

// this adds in basepath and sets api version

const fileUrl = getFileUrl(config, noteId)

const fileResponse = await axios.post(fileUrl, file, {
   headers: {
      'OAuth-Token': accessToken,
   },
});

 
Has anybody had success doing this? Can you share your code?
Thanks
Parents
  • When I do this in php, I do the following:

            //////////////////////////////////////////////////////////   			
    		//Create note record - POST /<module>/:record
            //////////////////////////////////////////////////////////   			
    
    		$url = $base_url . "/Notes";
    		
    		$note_arguments = array(
    			"set_created_by" => true,
                "portal_flag" => false,
                "embed_flag" => false,
                "following" => false,
                "my_favorite" => false,
    		);
    		
    		$note_response = call($url, $oauth2_token_response->access_token, 'POST', $note_arguments);
    
            $note_id = $note_response->id;
    		
    		if ($note_id != "") {
    
    			//////////////////////////////////////////////////////////   			
    			//Upload note file - POST /Notes/<id>/file/filename
    			//////////////////////////////////////////////////////////   			
    			
    			$url = $base_url . "/Notes/".$note_id."/file/filename";
    			
    			if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
    				$filedata = new CURLFile(realpath($dir.$data[0]),"",$data[2]);
    			} else {
    				$filedata = '@'.realpath($dir.$data[0]);
    			}
    			$file_arguments = array(
    				"format" => "sugar-html-json",
    				"delete_if_fails" => true,
    				"oauth_token" => $oauth2_token_response->access_token,
    				'filename' => $filedata,
    			);
    			$file_response = call($url, $oauth2_token_response->access_token, 'POST', $file_arguments, false,false,true);
    		}
    

    The call function (grown over years):

    /**
     * Generic function to make cURL request.
     * @param $url - The URL route to use.
     * @param string $oauthtoken - The oauth token.
     * @param string $type - GET, POST, PUT, DELETE. Defaults to GET.
     * @param array $arguments - Endpoint arguments.
     * @param array $encodeData - Whether or not to JSON encode the data.
     * @param array $returnHeaders - Whether or not to return the headers.
     * @param array $filenHeader - Whether or not to upload a file
     * @return mixed
     */
    function call(
        $url,
        $oauthtoken='',
        $type='GET',
        $arguments=array(),
        $encodeData=true,
        $returnHeaders=false,
    	$fileHeader=false
    )
    {
        $type = strtoupper($type);
    
        if ($type == 'GET')
        {
            $url .= "?" . http_build_query($arguments);
        }
    
        $curl_request = curl_init($url);
    
        if ($type == 'POST')
        {
            curl_setopt($curl_request, CURLOPT_POST, 1);
        }
        elseif ($type == 'PUT')
        {
            curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
        }
        elseif ($type == 'DELETE')
        {
            curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
        }
    
        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 0);  // wichtig
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);  // wichtig
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
    
        if (!empty($oauthtoken)) 
        {
    		if ($fileHeader) {
    			curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    				"oauth-token: {$oauthtoken}"));
    		} else {
                curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    				"oauth-token: {$oauthtoken}",
    				"Content-Type: application/json"));
    		}		
        }
        else
        {
            curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    			"Content-Type: application/json"));
        }
    
        if (!empty($arguments) && $type !== 'GET')
        {
            if ($encodeData)
            {
                //encode the arguments as JSON
                $arguments = json_encode($arguments);
            }
            curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
        }
    
        $result = curl_exec($curl_request);
    	
        if ($returnHeaders)
        {
            //set headers from response
            list($headers, $content) = explode("\r\n\r\n", $result ,2);
            foreach (explode("\r\n",$headers) as $header)
            {
                header($header);
            }
    
            //return the nonheader data
            return trim($content);
        }
    
        curl_close($curl_request);
    
        //decode the response from JSON
        $response = json_decode($result);
    
        return $response;
    }

Reply
  • When I do this in php, I do the following:

            //////////////////////////////////////////////////////////   			
    		//Create note record - POST /<module>/:record
            //////////////////////////////////////////////////////////   			
    
    		$url = $base_url . "/Notes";
    		
    		$note_arguments = array(
    			"set_created_by" => true,
                "portal_flag" => false,
                "embed_flag" => false,
                "following" => false,
                "my_favorite" => false,
    		);
    		
    		$note_response = call($url, $oauth2_token_response->access_token, 'POST', $note_arguments);
    
            $note_id = $note_response->id;
    		
    		if ($note_id != "") {
    
    			//////////////////////////////////////////////////////////   			
    			//Upload note file - POST /Notes/<id>/file/filename
    			//////////////////////////////////////////////////////////   			
    			
    			$url = $base_url . "/Notes/".$note_id."/file/filename";
    			
    			if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
    				$filedata = new CURLFile(realpath($dir.$data[0]),"",$data[2]);
    			} else {
    				$filedata = '@'.realpath($dir.$data[0]);
    			}
    			$file_arguments = array(
    				"format" => "sugar-html-json",
    				"delete_if_fails" => true,
    				"oauth_token" => $oauth2_token_response->access_token,
    				'filename' => $filedata,
    			);
    			$file_response = call($url, $oauth2_token_response->access_token, 'POST', $file_arguments, false,false,true);
    		}
    

    The call function (grown over years):

    /**
     * Generic function to make cURL request.
     * @param $url - The URL route to use.
     * @param string $oauthtoken - The oauth token.
     * @param string $type - GET, POST, PUT, DELETE. Defaults to GET.
     * @param array $arguments - Endpoint arguments.
     * @param array $encodeData - Whether or not to JSON encode the data.
     * @param array $returnHeaders - Whether or not to return the headers.
     * @param array $filenHeader - Whether or not to upload a file
     * @return mixed
     */
    function call(
        $url,
        $oauthtoken='',
        $type='GET',
        $arguments=array(),
        $encodeData=true,
        $returnHeaders=false,
    	$fileHeader=false
    )
    {
        $type = strtoupper($type);
    
        if ($type == 'GET')
        {
            $url .= "?" . http_build_query($arguments);
        }
    
        $curl_request = curl_init($url);
    
        if ($type == 'POST')
        {
            curl_setopt($curl_request, CURLOPT_POST, 1);
        }
        elseif ($type == 'PUT')
        {
            curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
        }
        elseif ($type == 'DELETE')
        {
            curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
        }
    
        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 0);  // wichtig
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);  // wichtig
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
    
        if (!empty($oauthtoken)) 
        {
    		if ($fileHeader) {
    			curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    				"oauth-token: {$oauthtoken}"));
    		} else {
                curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    				"oauth-token: {$oauthtoken}",
    				"Content-Type: application/json"));
    		}		
        }
        else
        {
            curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    			"Content-Type: application/json"));
        }
    
        if (!empty($arguments) && $type !== 'GET')
        {
            if ($encodeData)
            {
                //encode the arguments as JSON
                $arguments = json_encode($arguments);
            }
            curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
        }
    
        $result = curl_exec($curl_request);
    	
        if ($returnHeaders)
        {
            //set headers from response
            list($headers, $content) = explode("\r\n\r\n", $result ,2);
            foreach (explode("\r\n",$headers) as $header)
            {
                header($header);
            }
    
            //return the nonheader data
            return trim($content);
        }
    
        curl_close($curl_request);
    
        //decode the response from JSON
        $response = json_decode($result);
    
        return $response;
    }

Children
No Data