0

Good afternoon, I have been doing a small development in C #, for the request in HTTP POST.

I have the following problem, when trying in c # the following have not worked for me

using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace App_Llamadas_API
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [Obsolete]
        private void button1_Click(object sender, EventArgs e)
        {
            //URL:
            var client = new RestClient("Hidden URL");
            var request = new RestRequest("/", Method.POST);

            //Headers:
            request.AddHeader("X-Authorization", "Hidden Token");
            request.AddHeader("Content-Type", "application/json");

            //Body json:
            request.AddParameter(
            "application/json",
            "{ \"taskRelativePath\": \"My Tasks\\VPN.atmx\", \"botRunners\": [{ \"client\": \"DESKTOP -Hidden name\", \"user\": \"botrunner03\"}], \"runWithRDP\": \"true\" }", // <- your JSON string
             ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);

            var content = response.Content;

            txtResponse.Text = content;

        }

    }
}

reply: {"code":"json.deserialization.exception","details":null,"message":"Invalid request parameters"}

Agandara
  • 1
  • 1
  • 1
  • JSON does not allow backslashes. Try: '{ "taskRelativePath": "My Tasks\\VPN.atmx", "botRunners": [{ "client": "DESKTOP -Hidden name", "user": "botrunner03"}], "runWithRDP": "true" }' – samvdst Feb 05 '20 at 14:20
  • \"runWithRDP\": \"true\" my first guess is that this parameter is expecting a boolean value and you are passing in a string value. Change this to \"runWithRDP\": true and rerun – Travis Acton Feb 05 '20 at 14:47
  • I have tried but it has not worked, I have tried with another api before and it has worked in this way: https://ibb.co/Dw0vHMF option one works, option 2 does not work – Agandara Feb 05 '20 at 14:50
  • https://ibb.co/CbKNdky I have tried with TravisActon, look the same error appears again – Agandara Feb 05 '20 at 14:57
  • I tried this on postman and it works correctly https://ibb.co/XSZBs3v – Agandara Feb 05 '20 at 15:10

1 Answers1

0

The request is invalid because the JSON is improperly escaped. You need to escape each of the backslashes in My Tasks\\VPN.

"{ \"taskRelativePath\": \"My Tasks\\\\VPN.atmx\", \"botRunners\": [{ \"client\": \"DESKTOP -Hidden name\", \"user\": \"botrunner03\"}], \"runWithRDP\": \"true\" }");

Otherwise it will read the JSON as the following (notice there's only one backslash):

{
  "taskRelativePath": "My Tasks\VPN.atmx",  <-- invalid JSON
  "botRunners": [
    {
      "client": "DESKTOP -Hidden name",
      "user": "botrunner03"
    }
  ],
  "runWithRDP": "true"
}

With RestSharp, you may want to use request.AddJsonBody to construct your JSON payloads, since this is the kind of thing it was made for and you don't have to worry about quotes. This will use SimpleJson to serialize the object.

request.AddJsonBody(new
{
    taskRelativePath = "My Tasks\\\\VPN.atmx",
    botRunners = new[]
    {
        new { client = "DESKTOP -Hidden name", user = "botrunner03" }
    },
    runWithRDP = "true",
});

Roth
  • 149
  • 2
  • 7