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;
}
}