POST form params In API REST

0

I have to store the data in a BD that sends me an API, this API sends me a POST request to my server with the data as parameters Literal the documentation tells me that "the following parameters as form params"

{
  "solicitud": {
  "dato1": "1",
  "dato2": "2",
  "dato1": "3",
  .
  .
  .
  "datoN": "N",
 }
}

I want to take N data for example and store it, but I can not do it. My controller has the method as follows

[HttpPost]
public string PostConfir(Dictionary<string, string>solicitud)
{
 Ads.GuardaDat("Guarda", "dato24");//Aquí se manda a la BD para provar que si llega al post

        try//Intento trabajar el diccionario 
        {
            Dictionary<string, string> respS = solicitud["solicitud"];

            string dat = solicitud["dato1"];//Esto me genera error 
            Ads.GuardaDat(dat, "dato25");
        }

        catch (Exception ex)
        {
            Ads.GuardaDat(ex.Message, "dato25"); //Esto se guarda en BD si hay error
        }


        return "";
    }

How can I work with the data or data? Do you have to see the word form params refers to some specific parameter?

    
asked by Darian25 12.10.2018 в 17:02
source

1 answer

0

to use REST would include the Json.NET library that allows me to perform the following conversion:

REST value received (I assume this is what you receive):

 string jsonStr = "{\"solicitud\": {\"dato1\": 1,\"dato2\": 2,\"dato3\": 8}}";
 var jObjeto = JsonConvert.DeserializeObject<dynamic>(jsonStr);

now to access the values of your json:

var dato3 =  jObjeto["solicitud"]["dato3"];//8

then your driver would be as follows:

[HttpPost]
public void PostConfir(String solicitud){
 Ads.GuardaDat("Guarda", "dato24");//Aquí se manda a la BD para provar que si llega al post

        try
        {
            string jsonStr = solicitud;//"{\"solicitud\": {\"dato1\": 1,\"dato2\": 2,\"dato3\": 8}}";
             var jObjeto = JsonConvert.DeserializeObject<dynamic>(jsonStr);

            string dat = jObjeto["solicitud"]["dato25"];
            Ads.GuardaDat(dat, "dato25");
        }

        catch (Exception ex)
        {
            Ads.GuardaDat(ex.Message, "dato25");
        }
}

Now that if it's a Dictionary, what you get only converts this part then it's the same

var jObjeto = JsonConvert.DeserializeObject<dynamic>(solicitud["solicitud"]);

Do not forget to include the namespace: using Newtonsoft.Json;

Greetings

    
answered by 12.10.2018 / 18:36
source