2
votes

I need to post a request contains: a xml defined as a string and a json. I've problems with setting the content correctly and dealing with JToken.
The raw request looks like this in Fiddler:

Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468 User-Agent: Fiddler Authorization: Basic QURNSU46QURNSU4= Content-Length: 888 Host: localhost

---------------------------acebdf13572468 Content-Disposition: form-data; name="fieldNameHere"; filename="testfile2.txt" Content-Type: text/plain

This is a second test file used for file managment tests in the eB EC Plugin test environment. ---------------------------acebdf13572468 Content-Disposition: form-data; name="fieldNameHere" Content-Type: application/json

{ "instance": { "className": "File", "schemaName": "EB", "relationshipInstances": [ { "direction": "backward", "className": "DocumentFiles", "schemaName": "EB", "relatedInstance": { "className": "Document", "schemaName": "EB", "instanceId": "4" } } ], "properties": { "Name": "testfile2.txt" } } }

---------------------------acebdf13572468--

I have this method and got 404 error:

     public static string PostXMLStringAndJasonInOneRequest(int instanceId)
    {
        // Creating json that describes document with property 'name' 
        JObject documentToPost = CreateClassInstance("File", "eB", null, new KeyValuePair<string, object>("Name", "MyTestDocument.txt"));

        JObject relationship = CreateRelationshipClassInstance("DocumentFiles", "eB", null, "backward");

        // specifying document parent that is existing project with its id 
        JObject documentParent = CreateClassInstance("Document", "eB", instanceId.ToString());

        JToken json = CreateJson(documentToPost, relationship, documentParent);
        Uri uri = new Uri("http://localhost/wsg/v2.0/xxxx");

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", "Basic QURNSU46QURNSU4=");
            MultipartFormDataContent form = new MultipartFormDataContent();
            // TO-DO: save the xml string contents here
            HttpContent content = new ByteArrayContent(GetBytes("<node>this is a text</node>"));

            // HttpContent content = new StringContent("<node>this is a text</node>");
            content.Headers.Add("Content-Type", "application/xml");

            form.Add(content);

            // TO-DO Save json  - not hard coded the value of json
            string jsonText = @"{
                  ""instance"": {
                    ""className"": ""File"",
                    ""schemaName"": ""EB"",
                    ""relationshipInstances"": [
                       {
                          ""direction"": ""backward"",
                          ""className"": ""DocumentFiles"",
                          ""schemaName"": ""EB"",
                          ""relatedInstance"": {
                             ""className"": ""Document"",
                             ""schemaName"": ""EB"",
                             ""instanceId"": ""120""
                          }
                       }
                    ],
                    ""properties"": {
                        ""Name"": ""testfile2.xml""
                    }
                  }
                }";

            HttpContent jsonContent = new ByteArrayContent(GetBytes(jsonText));
            jsonContent.Headers.Add("Content-Type", "application/json");
            // TO-DO put json not jsonText
            form.Add(jsonContent);
            HttpResponseMessage response = client.PostAsync(uri, form).Result;
            if (response.StatusCode != HttpStatusCode.Created)
            {
                return "error"; // handle error 
            }
            return response.Content.ReadAsStringAsync().Result;
        }
    }
1
Please format your code properly. Does the 404 return any information?Yuval Itzchakov

1 Answers

1
votes
// To create multipartcontent 

    // 404 is an error in my url address

        private static HttpContent CreateMultipartContent(JToken json, Stream file, string fileName)
                {
                    MultipartContent content = new MultipartContent("form-data", Guid.NewGuid().ToString());

                    StringContent jsonPart = new StringContent(json.ToString());
                    jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
                    jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    StreamContent filePart = new StreamContent(file);
                    filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                    filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
                    filePart.Headers.ContentDisposition.FileName = fileName;

                    content.Add(jsonPart);
                    content.Add(filePart);

                    return content;
                }    
    private static HttpContent CreateMultipartContent(JToken json, string markupText, string fileName)
                        {
                            MultipartContent content = new MultipartContent("form-data", Guid.NewGuid().ToString());

                            StringContent jsonPart = new StringContent(json.ToString());
                            jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
                            jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                            StringContent filePart = new StringContent(markupText);
                            filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                            filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
                            filePar`enter code here`t.Headers.ContentDisposition.FileName = fileName;

                            content.Add(jsonPart);
                            content.Add(filePart);

                            return content;
                        }