You can post form values from a C# client to a REST-enabled ViewModel in MDriven Turnkey by sending multipart/form-data to the TurnkeyRest/Post endpoint.
Before you send a request
Prepare the receiving ViewModel as described in HowTos:Expose REST Service. In particular:
- Set the ViewModel tagged value
RestAllowed. - Use a root object reference as the
idparameter. - Declare the receiving ViewModel variables or columns as
Stringwhen you want to receive posted form values. - Ensure that the caller is allowed by the ViewModel access groups.
For a ViewModel named Consume, with receiving fields named Amount and Message, the endpoint has this form:
https://yoursystem/TurnkeyRest/Post?command=Consume&id=<complete-root-object-reference>
The command value is the ViewModel name. Always send a complete object ID, such as a GUID or xx|yyyy; do not rely on a shortened ID without a class identifier. See HowTos:Expose REST Service for supported ID formats and routes.
Post multipart form data from C#
Use MultipartFormDataContent and give it a boundary token. The same boundary must be present in the request Content-Type header. TurnkeyRest uses the multipart boundary to parse the posted fields.
var cli = new System.Net.Http.HttpClient();
var requestContent = new System.Net.Http.MultipartFormDataContent("--BOUNDARY");
// The second argument is the form-field name.
// It must match the receiving ViewModel variable or column name.
requestContent.Add(
new System.Net.Http.StringContent("DataToAmountVMCol"),
"Amount");
requestContent.Add(
new System.Net.Http.StringContent("DataToMessageVMCol"),
"Message");
requestContent.Headers.ContentType =
System.Net.Http.Headers.MediaTypeHeaderValue.Parse(
"multipart/form-data; boundary=--BOUNDARY");
var url = "https://yoursystem";
var rootObjectReference = "<complete-root-object-reference>";
var res = cli.PostAsync(
url + "/TurnkeyRest/Post?command=Consume&id=" + rootObjectReference,
requestContent).Result;
if (res.IsSuccessStatusCode)
{
// Read the returned ViewModel [[Documentation:JSON|JSON]] if the calling code needs it.
var responseBody = res.Content.ReadAsStringAsync().Result;
}
In this example, the receiving ViewModel gets these values:
| Posted form-field name | Posted value | Receiving ViewModel field |
|---|---|---|
Amount
|
DataToAmountVMCol
|
Amount
|
Message
|
DataToMessageVMCol
|
Message
|
For a POST, TurnkeyRest matches posted parameter names to ViewModel variables and applies the values. It then executes root-level ViewModel actions in their displayed order and saves changed values. Root actions placed before columns run before posted values are applied; actions after columns run afterwards. See Documentation:Rest Post for action ordering.
Keep the multipart boundary intact
The boundary token is required both when creating MultipartFormDataContent and in the Content-Type header. If the request does not provide a usable multipart boundary, TurnkeyRest cannot parse the form values.
Use one boundary token consistently for one request. In the example, that token is --BOUNDARY.
Read the response
A successful TurnkeyRest POST returns the ViewModel content as JSON by default. Check res.IsSuccessStatusCode before treating the request as successful, then read the response body when your integration needs the returned data.
A REST-enabled ViewModel can override the returned content and status by using vReturnMessage:String and vReturnStatusCode:String. For example, an action can set vReturnStatusCode:='Created'. See HowTos:Expose REST Service for the return-value contract.
Authentication and access
TurnkeyRest checks the ViewModel access groups after it finds the ViewModel and root object. If the service requires authentication, send credentials appropriate to your Turnkey setup. TurnkeyRest supports HTTP Basic authentication in the Authentication request header, and Documentation:Log in with code describes programmatic login and REST authentication.
When the posted fields are not known in advance
The example above is for known fields such as Amount and Message. If an external system can send an unknown number of fields or uploaded files, model the FormFields and/or FormFiles pattern instead of attempting to declare every possible field. See Documentation:Receive post data not known at design time.
Troubleshooting
| Symptom | Check |
|---|---|
| ViewModel fields are not populated | Verify that the request is multipart/form-data, that its boundary is supplied consistently, and that each form-field name exactly matches a receiving ViewModel variable or column.
|
| The request cannot find the intended REST service | Verify the command ViewModel name, the host URL, and the complete id object reference.
|
| Access is denied | Verify RestAllowed, the ViewModel access groups, and the authentication supplied by the client.
|
| The result is not the default ViewModel JSON | Check whether the receiving ViewModel sets vReturnMessage, vReturnStatusCode, or a root attribute named RawJSon.
|
When diagnosing the outgoing HTTP request, post the same request to postman-echo.com to inspect the form data and headers before sending it to TurnkeyRest.
