jsonhttprequestlotus-noteslotus-dominoibm-domino

Domino Data Service - change name of attached file in rich-text-field


I have an annoying problem and I am not sure anyone knows the answer, but asking cost nothing.

I am attaching a PDF-File via a PATCH-Methode to the Server with the Domino-Data-Service Rest-API. The attaching does work properly, but the attachment in the rich-text-field does have a automatically generated name.

My question: How can I change the name of this file by attaching it.

I wrote my code in C#, but I think this is not an programming language depending problem.

My code for attaching the PDF (C#):

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes({username:passwort})));

Byte[] bytes = File.ReadAllBytes(@"H:\jakob.pdf");
String file = Convert.ToBase64String(bytes);

var json = "{" + 
           "\"anmerkung\":{" +
           "\"type\":\"multipart\"," +
           "\"content\":[ {" +
                      "\"contentType\":\"application/pdf\"," +
                      "\"data\":\"" + file + "\"," +
                      "\"contentTransferEncoding\":\"base64\"" +
                                                           "}" +
                                                           "]" +
                          "}" +
           "}";

var data = new StringContent(json, Encoding.UTF8, "application/json");
var url = @"https://{database}/api/data/documents/unid/424CC34CFB0746F6C12585A7003B277B?computewithform=true&form={formname}";

var response = await client.PatchAsync(url, data);

string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
client.Dispose();

This works fine but my PDF looks like this:

enter image description here

I want the pdf file to have the name jakob.pdf

So I tried to rebuild the json, in the way the json is build when using a GET-Request to a document with a PDF file in a rich-text-field

My code for attaching the PDF, with the rebuild json(C#):

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes({username:passwort})));

Byte[] bytes = File.ReadAllBytes(@"H:\jakob.pdf");
String file = Convert.ToBase64String(bytes);


var json2 = "{" +
            "\"anmerkung\":{" +
            "\"type\":\"multipart\"," +
            "\"content\":[ {" +
                       "\"contentType\":\"application/pdf; name=\"jakob.pdf\"\","+
                       "\"contentDisposition\":\"inline; filename=\"jakob.pdf\"\","+
                       "\"data\":\"" + file + "\"," +
                       "\"contentTransferEncoding\":\"base64\"" +
                                                            "}" +
                                                            "]" +
                          "}" +
            "}";

var data = new StringContent(json2, Encoding.UTF8, "application/json");
var url = @"https://{database}/api/data/documents/unid/424CC34CFB0746F6C12585A7003B277B?computewithform=true&form={formname}";

var response = await client.PatchAsync(url, data);

string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
client.Dispose();

When I' m using this piece of code I always get an error-message:

"code":400,
"text":"Bad Request",
"message":"Fehler beim Parsen der JSON-Inhalte",
"type":"text",
"data":"com.ibm.domino.services.ServiceException: Fehler beim Parsen der JSON-Inhalte\r\n\tat

Regarding to this peice of code I am not even sure, if this code would normally work it was just an idea, that I tried.

I know this is a very complex and difficult problem, but maybe somone knows the answer to this problem or has some idea to fix this problem.

thanks in advance!


Solution

  • The most likely reason for the error response is that, when you set the filename in your JSON, you have created malformed JSON.

    To take a single line of your code as an example, this line is malformed JSON because of the quote characters around the filename:

        "\"contentType\":\"application/pdf; name=\"jakob.pdf\"\","+
    

    The line with contentDisposition has the same problem.

    I highly recommend using an API designed to turn a structured object into proper serialized JSON, which should make it easy to avoid the problem, but I don't know what is available in C# for this purpose.

    The list at the bottom of the following page may help: https://www.json.org/json-en.html