1 Reply Latest reply on Mar 30, 2016 2:14 AM by Cennovate

    Using C# to access REST API - 404 Not Found

    Cennovate

      Hello All!

       

      Since there are currently no C# examples available for accessing the REST API, I am going at this from scratch using the Nuget Packet - Microsoft.AspNet.WebApi.Client.

       

      I have been able to successfully setup a framework of code that is able to make a call to the base_uris method and successfully receive a response.

       

      I am trying to make a call transientDocuments, but I am continually getting a 404 Not Found error.  I am hoping somewhere here may be able to shed some light.  I have been trying to use the current Java examples for calling the REST API to get some help on how to go about things.

       

      I create an instance of the AdobeDCREST class and pass in the BaseUrl (https://api.echosign.com/api/rest/v5/)  and my Integration Key.  When I make a call to the PostTransientDocuments method I pass in the filePath of the pdf I am trying to upload.  PostTransientDocuments then takes care of creating an HttpContent (StreamContent) object from the file and then adding the ContentType and ContentDisposition headers.  The call to GetClient() will determine if base_uris needs to be called yet or not (I am making the call to base_uris and storing the api_access_point in a static variable for further calls, just like the java examples) and will return an HttpClient object with the Access-Token in the header already.  The call to base_uris works and returns an api_access_point of https://api.na1.echosign.com/

       

      I then append /api/rest/v5 to the access_point url for calling transientDocuments. The failure occurs on the PostAsync call to transientDocuments with a 404 Not Found.

       

      Here is the code I am using so far to test things out.  Any help would be greatly appreciated.  I have been spinning my wheels on this for too long already.

       

      using System;

      using System.Collections.Generic;

      using System.IO;

      using System.Linq;

      using System.Net.Http;

      using System.Net.Http.Headers;

      using System.Text;

      using System.Threading.Tasks;

       

       

      namespace Ivezt.Documents {

          public class AdobeDCREST {

              /// <summary>

              /// Static variable which is set by initial call to GetBaseURIs() and used for all API calls thereafter.

              /// </summary>

              private static string SERVICES_BASE_URL = string.Empty;

              /// <summary>

              /// The API end point to use.  This would need to be changed if Adobe moves to a new version of the API and we update this

              /// code use that new version.

              /// </summary>

              private static string API_URL = "api/rest/v5/";

              /// <summary>

              /// This BaseUrl is passed in to the constructor and used to make a call to GetBaseURIs().

              /// </summary>

              private string m_strBaseUrl = string.Empty;

              /// <summary>

              /// The IntegrationKey is passed into the constructor and must be added to the header of every API request.

              /// </summary>

              private string m_strIntegrationKey = string.Empty;

       

       

              public AdobeDCREST(string strBaseUrl, string strIntegrationKey) {

                  m_strBaseUrl = strBaseUrl;

                  m_strIntegrationKey = strIntegrationKey;

              }

       

       

              private HttpClient GetDefaultClient() {

                  HttpClient client = new HttpClient();

                  // Add an Accept header for JSON format.

                  client.DefaultRequestHeaders.Accept.Add(

                  new MediaTypeWithQualityHeaderValue("application/json"));

       

                  //Add the Access-Token

                  client.DefaultRequestHeaders.Add("Access-Token", m_strIntegrationKey);

       

                  return client;

              }

       

       

              private HttpClient GetClient() {

                  //If we don't have the SERVICES_BASE_URL yet, then we must make a call GetBaseURIs

                  if (SERVICES_BASE_URL.Length == 0) {

                      HttpClient baseClient = GetDefaultClient();

                      //Use the BaseUrl passed into the Constructor

                      baseClient.BaseAddress = new Uri(m_strBaseUrl);

                      BaseURIs_Response uris = GetBaseURIs(baseClient);

                      SERVICES_BASE_URL = string.Format("{0}{1}", uris.api_access_point, API_URL);

                      baseClient.Dispose();

                  }

       

                  if (SERVICES_BASE_URL.Length == 0)

                      throw new Exception("Unable to retrieve Adobe Document Cloud Base URIs");

       

                  HttpClient client = GetDefaultClient();

                  client.BaseAddress = new Uri(SERVICES_BASE_URL);

       

                  return client;

              }

       

       

              public BaseURIs_Response GetBaseURIs(HttpClient client) {

                  //Call base_uris

                  HttpResponseMessage response = client.GetAsync("base_uris").Result;  // Blocking call!

                  if (response.IsSuccessStatusCode) {

                      // Parse the response body. Blocking!

                      return (BaseURIs_Response)response.Content.ReadAsAsync(typeof(BaseURIs_Response)).Result;

                  }

                  else {

                      throw new Exception(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));

                  }

              }

       

       

              public TransientDocument_Response PostTransientDocuments(string strFilePath) {

                  //Call transientDocuments

                  HttpContent content = new StreamContent(new FileStream(strFilePath, FileMode.Open, FileAccess.Read));

                  content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

                  content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {

                      Name = "File",

                      FileName = "Template.pdf"

                  };

       

                  HttpClient client = GetClient();

       

                  HttpResponseMessage response = client.PostAsync("transientDocuments", content).Result;  // Blocking call!

                  if (response.IsSuccessStatusCode) {

                      // Parse the response body. Blocking!

                      return (TransientDocument_Response)response.Content.ReadAsAsync(typeof(TransientDocument_Respons e)).Result;

                  }

                  else {

                      throw new Exception(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));

                  }

              }

          }

       

          public class BaseURIs_Response {

              public string web_access_point { get; set; }

              public string api_access_point { get; set; }

          }

       

          public class TransientDocument_Response {

              public string transientDocumentId { get; set; }

          }

      }

        • 1. Re: Using C# to access REST API - 404 Not Found
          Cennovate Level 1

          I believe I found my issue.  I was not making a multi-part form data post.  Once I changed that, everything worked as expected.  Here is an updated version of the PostTransientDocuments() method that works:

           

                  public TransientDocument_Response PostTransientDocuments(string strFilePath) {

                      //Call transientDocuments

                      using (var client = GetClient()) {

                          using (var content = new MultipartFormDataContent()) {

                              var fileContent = new StreamContent(new FileStream(strFilePath, FileMode.Open, FileAccess.Read));

                              fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

                              fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {

                                  Name = "File",

                                  FileName = "Template.pdf"

                              };

           

                              content.Add(fileContent);

           

                              HttpResponseMessage response = client.PostAsync("transientDocuments", content).Result;  // Blocking call!

                              if (response.IsSuccessStatusCode) {

                                  // Parse the response body. Blocking!

                                  return (TransientDocument_Response)response.Content.ReadAsAsync(typeof(TransientDocument_Respons e)).Result;

                              }

                              else {

                                  throw new Exception(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));

                              }

                          }

                      }

                  }