docusign-esign-csharp-client icon indicating copy to clipboard operation
docusign-esign-csharp-client copied to clipboard

How should I use EnvelopesApi.UpdateDocument?

Open eg-bernardo opened this issue 3 years ago • 9 comments

I'm talking about this method: https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopedocuments/update/ There it says:

The bytes of the document make up the body of the request

but the signature of the method is:

public EnvelopeDocument UpdateDocument(string accountId, string envelopeId, string documentId)

Calling that method as is, returns:

{
  "errorCode": "INVALID_REQUEST_PARAMETER",
  "message": "The request contained at least one invalid parameter. A filename was not found in the Content-Disposition header ('filename=\"filename.ext\""
}

I've also tried to provide the file by replacing the RestClient, and adding this to the IRestRequest using:

req.AddHeader("Content-Disposition", "filename=\"my_file.docx\"");
req.AddFile("my_file.docx", pathToFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

but then I get:

{
  "errorCode": "FORMAT_CONVERSION_ERROR",
  "message": "The data could not be converted."
}

eg-bernardo avatar Dec 16 '21 11:12 eg-bernardo

Hello, I'm Gloriana from DocuSign Developer Support.

I'll be happy to assist with this, would you be able to provide a complete code snippet (without exposing any sensitive information) so we can have better context for this issue? Thank you.

GlorianaMarinDS avatar Dec 17 '21 23:12 GlorianaMarinDS

The main issue is that EnvelopesApi.UpdateDocuments signature is wrong as far as I understand. As per https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopedocuments/update/, this method is used to update a file, but I cannot see how to provide it using EnvelopesApi.UpdateDocument.

var envelopeApi = new EnvelopesApi(apiClient);
// where do file bytes and name go?
var res = envelopeApi.UpdateDocument(accountId, "<envelope id>", "1");

eg-bernardo avatar Dec 21 '21 09:12 eg-bernardo

Hi @eg-bernardo, you are correct on this, the SDK method is wrong and has been reported internally as a bug. In this case, you will have to add new documents and delete old documents with the SDK method while this is looked into internally.

GlorianaMarinDS avatar Jan 04 '22 00:01 GlorianaMarinDS

and update on this?

niramber avatar Sep 15 '22 14:09 niramber

Hi @GlorianaMarinDS

I debugged this and found that the issue is with UpdateDocumentWithHttpInfo method inside UpdateDocument method call. The value is not set for localVarHttpContentDisposition = "form-data;name="\fileXml";filename="file.xml";

Not sure if the above is the correct value for a PDF. Please have the correct value in place and update the fix ASAP.

Below is the error we get while making call to UpdateDocument method:- Call - public EnvelopeDocument UpdateDocument(string accountId, string envelopeId, string documentId, byte[] documentFileBytes)

Exception - { "errorCode": "INVALID_REQUEST_PARAMETER", "message": "The request contained at least one invalid parameter. A filename was not found in the Content-Disposition header ('filename="filename.ext"" }

Thanks Gaurav

gchaudhary7472 avatar May 26 '23 13:05 gchaudhary7472

any updates on this?

esukmonica avatar Jul 23 '23 20:07 esukmonica

Resolving the bug such that EnvelopesApi.UpdateDocument correctly attempts to set the Content-Disposition header will reveal another far more insidious bug in SystemNetHttpClient.BuildRequestMessage where the line requestMessage.Content.Headers.Add("Content-Disposition", request.ContentDisposition); will fail if the content disposition string contains an equals sign (which it must for update document). The fix is to use Headers.TryAddWithoutValidation instead of Headers.Add.

aaston86 avatar Sep 22 '23 17:09 aaston86

@aaston86 Sure if that works well and good. @GlorianaMarinDS - any update as to when this will be fixed.

gchaudhary7472 avatar Sep 23 '23 06:09 gchaudhary7472

HT @lukasparsons for the collab...

In case it helps someone else, here is how we are working around this issue:

protected async Task UploadOrReplaceDocument(string envelopeId, string documentId, byte[] bytesToUpload, string fileName)
{
    HttpRequestMessage request;
    try
    {
        request = new HttpRequestMessage(HttpMethod.Put,
            $"{_requestItemsService.ApplicationSession.BasePath}/restapi/v2.1/accounts/{_docuSignJwtSettings.AccountId}/envelopes/{envelopeId}/documents/{documentId}");
        request.Headers.Add("Authorization", $"Bearer {_requestItemsService.ApplicationUser.AccessToken}");
        var content = new ByteArrayContent(bytesToUpload);
        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
        content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = fileName };
        request.Content = content;
        // If or when this GitHub issue is resolved, we may be able to switch to the following single line version of the above.
        // https://github.com/docusign/docusign-esign-csharp-client/issues/357
        //EnvelopeDocument response = await EnvelopesApi.UpdateDocumentAsync(_docuSignJwtSettings.AccountId, envelopeId, documentId, bytesToUpload);
    }
    catch (Exception e)
    {
        Log.Error("Error occurred preparing DocuSign request for uploading replacement pdf", e);
        throw;
    }

    // ... method continues
}

leifjones avatar Jan 22 '24 17:01 leifjones