C#C
C#3y ago
23 replies
euskater

Newtonsoft.Json.JsonReaderException: "Unexpected character encountered while parsing value: <. Path

Hei all, I am coding a REST API HTTP POST REQUEST, but I always get an error as soon as I start Building the Project. Might it be the way how my JSON is written ? or maybe something else ?

the code:

static private void RunAsyncPost(string username, string password)
{
            string url = "http://someIP/rest";

            // Create the JSON request body
            var requestBody = new
            {
                info = new
                {
                    name = "C#testfile.js",
                    type = 282,
                    isDir = false,
                    desc = "",
                    @lock = "",
                    ownerName = "Some Service",
                    access = "WDDRWDE-PIDDS",
                    parentId = 234253
                },
                acl = new[]
                {
                new
                {
                    member = "Administrator",
                    id = 0,
                    type = "USER",
                    access = "RWWEDSGEERLP"
                }
            },
                keywording = new
                {
                    maskNameOriginal = "Incoming Invoice",
                    fields = new
                    {
                        VENDOR_NAME = "Test_Vendor1"
                    }
                }
            };

            // Serialize the JSON request body
            string jsonRequestBody = JsonConvert.SerializeObject(requestBody);

            // Create the HTTP client and request message
            using (var httpClient = new HttpClient())
            using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, url))
            {
                // Set the authorization header
                string authHeader = "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"));
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Authorization", authHeader);

                // Set the content type and request body
                requestMessage.Content = new StringContent(jsonRequestBody, System.Text.Encoding.UTF8, "application/json");

                // Send the HTTP request and get the response
                var response = httpClient.SendAsync(requestMessage).Result;

                // Check the response status code
                if (response.IsSuccessStatusCode)
                {
                    // Deserialize the response body
                    //var responseBody = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
                    var responseBody = JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().Result);
                // Do something with the response
                ResponseStream = responseBody.ToString(); // output normaly in CMD but here I am using a textbox of windows Forms
                }
                else
                {
                ResponseStream = $"HTTP error {response.StatusCode}"; // output normaly in CMD but here I am using a textbox of windows Forms
                }
            }


Thanks in advance for any help 🙂
Was this page helpful?