PinBack
PinBack

Reputation: 2564

Web API post parameter

I currently have an issue with a webapi call. I want to download and open a logfile with my ApiController. I use a javascript function to post a filename to my controller.

Here is a helper function to post the parameter (answer from dystroy): How to replace window.open(...) with a POST

Now when I use a simple string as parameter in my controller I can’t get the parameter, it is always null.

public HttpResponseMessage PostDownloadLogFile([FromBody]string psFileName)
{
    //psFileName is always null

But if I use HttpReqeustMessage as parameter and read the form data from my request it is no problem and it works.

public HttpResponseMessage PostDownloadLogFile(HttpRequestMessage poRequest)
{
    var loFormData = poRequest.Content.ReadAsFormDataAsync().Result;
    string psFileName = loFormData["psFileName"]; //psFileName is set correct

Is there a solution to get the parameter with a simple parameter in my controller?

Update

This is my javascript helper function:

var loOpenWindow = function (psMethode, psUrl, poData, psTarget) {
    var loForm = document.createElement("form");
    loForm.action = psUrl;
    loForm.method = psMethode;
    loForm.target = psTarget || "_self";
    if (poData) {
        for (var lsKey in poData) {
            var loInput = document.createElement("textarea");
            loInput.name = lsKey;
            loInput.value = typeof poData[lsKey] === "object" ? JSON.stringify(poData[lsKey]) : poData[lsKey];
            loForm.appendChild(loInput);
        }
    }
    loForm.style.display = "none";
    document.body.appendChild(loForm);
    loForm.submit();
};

Call it:

helper.openWindow("POST", apiRoutes.URLS.ApiPostDownloadLogFile, { "psFilename": $scope.data.showLogEntry.FullName });

There should be no problem from the client side code, because the controller methode with HttpReqeustMessage works without problems.

Here is the browser request: enter image description here

Upvotes: 2

Views: 381

Answers (2)

PinBack
PinBack

Reputation: 2564

Ok I found a solution. If I use a class as parameter and a property with the given name, it seems to work.

public class Param
{
    public string psFileName { get; set; }
}

And

public HttpResponseMessage PostDownloadLogFile(Param poParam)
{
    string psFileName = poParam.psFileName; //psFileName is set correct

This is not really a simple parameter but I can live with this solution.

Upvotes: 1

Filipe Borges
Filipe Borges

Reputation: 2793

Probably the problem is in your client code sending the data.

[FromBody] parameters must be encoded as =value

then, this does not work:

// Value will be null.
$.post('api/values', value);

// Value will be null.
$.post('api/values', { key: value });

But this work:

$.post('api/values', "=" + value);

Try to change your client code to send just =/path/to/your/file in the body.

Reference: http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

Upvotes: 2

Related Questions