Click here to Skip to main content
14,828,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
I try to bring the pdf file into a standard software, wrote the code, an exception is issued because of wrong format with json although json string corresponds exactly to the specified json of api call of standard software. can you please take a look if I wrote something wrong here or if I forgot something? I covered some information with a star Many Thanks

What I have tried:

C#
IRestResponse response3;

            client = new RestClient("http://***.***.***.*");
            client.Authenticator = new HttpBasicAuthenticator("********", "*********");
            client.Timeout = -1;
            request = new RestRequest("/*****/api/******/*****/***", Method.POST);
            request.AddHeader("content-type", "multipart/form-data");
            request.AlwaysMultipartFormData = true;

            request.AddParameter("Object", "{" +
                    "\"cabinet\":\"Posteingang\"," +
                    "\"name\":\"Posteingang\"," +
                    "\"objectTypeId\":\"2\"," +
                    "\"fields\":{" +
                    "\"Datum\":{\"value\":\"" + DateTime.Now.ToString("dd.MM.yyyy") + "\"}" +
                    "}" +
                "}");
            request.AddFile("file","C:/Users/*********/Documents/*********/Org.pdf","Org.pdf");

            MultipartFormDataContent multipartForm = new MultipartFormDataContent();
            byte[] file_bytes = File.ReadAllBytes("C:/Users/********/Documents/********/Org.pdf");

            multipartForm.Add(new ByteArrayContent(file_bytes, 0, file_bytes.Length), "profile_pic", "hello1.jpg");


            multipartForm.Add(new StringContent("Object"), "{" +
            "\"cabinet\":\"Posteingang\"," +
            "\"name\":\"Dokument\"," +
            "\"objectTypeId\":\"******\"," +
            "\"fields\":{" +
            "\"Datum\":{\"MAIL_SUBMIT_TIME\":{\"value\":\"" + DateTime.Now.ToString("dd.MM.yyyy") + "\"},\"Typ\":" +
            "{\"feld4\":{\"value\": \"Brief\"}},\"Absender\": {\"Mail_FROM\":{\"value\": \"*********\"}}}}}");

            response3 = client.Execute(request);
            Console.WriteLine(response3.Content);
        }  
    }
}
Posted
Updated 5-Mar-21 2:36am
v3
Comments
Richard MacCutchan 5-Mar-21 7:57am
   
You need to check with the owner of the API, as we have no idea what the requirements are. Is feld4 a correct name?

1 solution

Start by using the debugger - or logging to a file - to find out exactly what you are sending as "valid JSON" and run it though a JSON-to-C# class creator (VS has one, just use the Edit menu, "Paste special...Paste JSON as Classes" and VS will create the classes it represents for you.
You can then use the string to populate those classes via a JSON reader and check exactly what it gives you.

But I suspect that your manually generated JSON has bad data in it - you would be better off creating JSON via a "Proper" class-to-JSON package than "brewing it yourself" like that.
   
Comments
FWK9 5-Mar-21 10:06am
   
I will follow your advice, thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900