Accessing a list/array in a JSON URL

Asked

Viewed 241 times

3

I am sending via JSON an array of strings, how can I access this array via url? example of some of the tests:

http://localhost:51746/api/Language/Getlanguagelist? key=[spool,error] http://localhost:51746/api/Language/Getlanguagelist? key={racket,}

But it didn’t work, someone knows how I can do it?

[HttpGet]
public HttpResponseMessage GetLanguageList(List<string> keys)
{
    try
    {
        #region meu codigo

        Dictionary<string, string> key_value = new Dictionary<string, string>();


        foreach (string key in keys)
        {
            key_value.Add(key,Resources.Language.ResourceManager.GetString(key));
        }

        #endregion

        return Request.CreateResponse(HttpStatusCode.OK, new ResponseApi()
        {
            Status = Status.OK,
            Message = "Sucesso",
            Response = key_value
        },
        "application/json");
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.OK, new ResponseApi()
        {
            Status = Status.NOK,
            Message = ex.Message,
            Response = null
        },
        "application/json");
    }
}

1 answer

1

Just repeat the name of the list variable that appears in your Controller, in your case keys, when the controller receives the request it will know it is a list and automatically convert.

http://localhost:51746/api/Language/GetLanguageList?keys=carregando&keys=erro

If you want to send JSON you will need to use the POST method, not send Whole Object (in your case JSON) via GET.

  • I tried here, but the Getlanguagelist(List<string> Keys) parameter is null, even if I say so. Do I have to use an array ? string [] Keys?

  • 1

    With array or list it would understand the same way...

  • 1

    you copied and pasted my url into your browser?

  • Yes, I also tried without copying and pasting. Even passing only one parameter the return is coming null

  • I had to concatenate all the strings and give a split (famous gambiarra), because I spent all day searching and did not find the solution :/ , if someone knows and can help , thanks

  • 1

    @Guilhermeprado also researched if there was a case like yours... and I could find nothing, mals.

  • Quiet, thank you very much, I will leave in Gambi even kkkkkk But thank you very much for the attention!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.