0

I am unable to work out how to post a string array to my APIController

I want to be able to send the following JSON string:

{"userName":"un","userPassword":"password"}

This is my Controller:

public class CheckAuthenticationController : ApiController
{
    public object Post(string[] stuff)
    {
        try
        {
            return GeneralFunctions.CheckAuthentication(stuff[0].ToString(), stuff[0].ToString());
        }
        catch
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NoContent));
        }
    }
}

This what my post looks like in fiddler:

POST https://localhost:8081/CheckAuthentication HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:8081
Content-Length: 43

{"userName":"un","userPassword":"password"}

But when I debug, "stuff" is always null.

Can anyone help please?

1
  • how are you posting it? you are expecting an array... Commented Apr 24, 2014 at 14:03

1 Answer 1

2

That is a javascript object(json) and not a string array.

A string array in javascript will look like this:

["this","is","a","string","array"]

and an object will look like:

{this:"is", a:"string", object:"ok"}

You will need to add a class like this:

class LoginData
{
   public string Username {get;set;}
   public string UserPassword {get;set;}
}


public class CheckAuthenticationController : ApiController
{
    public object Post(LoginData loginData)
    {
        try
        {
            return GeneralFunctions.CheckAuthentication(loginData.Username , loginData.UserPassword );
        }
        catch
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NoContent));
        }
    }
}

and this should be your json post:

{Username : 'user', UserPassword: 'pass'}
Sign up to request clarification or add additional context in comments.

2 Comments

{Username... is not valid JSON. It should have double quotes, like his original JSON
Your'e right, I wrote plain javascript. The json string will appear a bit different in the post data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.