locked
Pass JsonString to HTTP GET Method in API RRS feed

  • Question

  • User-1401876114 posted

    Hello

    I need to pass serialized Json string to GET method. Please pass some code snippets

    Thanks

    Friday, December 7, 2018 6:52 AM

All replies

  • User475983607 posted

    I need to pass serialized Json string to GET method. Please pass some code snippets

    By definition HTTP GET passes data in the URL.  While it is possible to pass a simple json string in the URL it is not recommended and requires manual serialization and deserialization.

    I recommend an HTTP POST to pass json.  Otherwise explain the problem you are trying to solve at a high level and we can provide a suitable approach.   Also include the GET method so we can have an idea of what you are doing and why.

    Friday, December 7, 2018 3:29 PM
  • User-474980206 posted

    as stated it requires custom serialization, and is limited in length:

    $.get(url, {
         json: JSON.srtingify(data)
    });
    

    is a form post, where the json string is in the parameter json. mvc action

    IActionResult GetJson (string json)
    {
        var myObject = JSON.DeserializeObject<MyObject>(json);
    } 

    Saturday, December 8, 2018 12:43 AM
  • User61956409 posted

    Hi  Shreenidhi,

    I need to pass serialized Json string to GET method. Please pass some code snippets

    Firstly, to achieve your requirement, you can refer to the following code snippets:

    [Route("api/sayhello")]
    public string Get(string name)
    {
        JToken valueToken = JObject.Parse(name);
    
        var mes = "Hello " + valueToken.SelectToken("firstname")?.ToString() + " " + valueToken.SelectToken("lastname")?.ToString();
        return mes;
    }

    Test Result:

    Besides, the [HttpGet] enables us to sent data in the URL. If you want to sent complex json data to your Web API action, as mgebhard suggested, you can try to use [HttpPost].

    With Regards,

    Fei Han

    Monday, December 10, 2018 7:12 AM