Error Json Line 0 position 0

0

I have already searched the site about this error and so far I have not found something that solves my problem. I'm trying to use restsharp, but I'm getting an error when passing data to .json

My Page with the API

<?php
header('Content-type: application/json');

require_once("config/config.php");

$result = array('status' => 500, 'message' => "Internal error");

if (isset($_POST['login']) && isset($_POST['password']))
    {
		$login = $_POST['login'];
        $password = $_POST['password'];
        $uuid = 'Not Found';
		
		if (validaUsuario($login, $password) == true) 
		{
            $result['status'] ='42';
            $result['message'] = 'Connected with success';
			$result['user'] = 'Teste';
            $result['level'] = '100';			

			echo json_encode($result);
            return;
			
        } else 
		{
			$result['status'] =202;
            $result['message'] = "Password or login incorrect";
			$result['token'] = 123456789;
            $result['level'] = 10;
			echo json_encode($result);
            return;
		}
            
			echo json_encode($result);

	}

?>

My code:

public static void UserLogin(string user,string pass)
    {
        var client = new RestClient();
        client.BaseUrl = new Uri("http://localhost/r_user.php");

        var request = new RestRequest();
        request.Method = Method.POST;

        request.AddParameter("login", user);
        request.AddParameter("password", pass);

        IRestResponse response = client.Execute(request);
        var content = response.Content;

        dynamic res = JObject.Parse(content);

        if (res.status == "42")
        {

        }
    }

I get the following error:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: . Path '', line 0, position 0.'

I'm getting the content like this: {"status":"42","message":"Connected with success","user":"Teste","level":"100"}

    
asked by anonymous 15.12.2018 / 23:04

1 answer

0

After several tests, I discovered that it was an easy problem to solve.

I was getting the visually correct data, copied the return on json I was getting and put it in json validator

In this validator I noticed that I had a space before the first key, to deserialize can not have a space in the front. I was using Notepad ++ and I changed the encoding to UTF8-Without BOM, but the problem remained, studying more I realized that all the files that my .php file uses need have the same encoding.

In my case I called another file:

require_once("config/config.php");

I changed the config.php condition and solved the problem.

    
16.12.2018 / 20:30