''>' is an unexpected token. The expected token is '"' or '''. Line 1, position 55
Below is the code . Trying to call a rest api but receiving the above error of unexpected token.
public static class Program
{
public static void Main()
{
Uri baseUrl = new Uri("https://httpbin.org/");
IRestClient client = new RestClient(baseUrl);
IRestRequest request = new RestRequest("get", Method.GET) { Credentials = new NetworkCredential("testUser", "P455w0rd") };
request.AddHeader("Authorization", "Bearer qaPmk9Vw8o7r7UOiX-3b-8Z_6r3w0Iu2pecwJ3x7CngjPp2fN3c61Q_5VU3y0rc-vPpkTKuaOI2eRs3bMyA5ucKKzY1thMFoM0wjnReEYeMGyq3JfZ-OIko1if3NmIj79ZSpNotLL2734ts2jGBjw8-uUgKet7jQAaq-qf5aIDwzUo0bnGosEj_UkFxiJKXPPlF2L4iNJSlBqRYrhw08RK1SzB4tf18Airb80WVy1Kewx2NGq5zCC-SCzvJW-mlOtjIDBAQ5intqaRkwRaSyjJ_MagxJF_CLc4BNUYC3hC2ejQDoTE6HYMWMcg0mbyWghMFpOw3gqyfAGjr6LPJcIly__aJ5__iyt-BTkOnMpDAZLTjzx4qDHMPWeND-TlzKWXjVb5yMv5Q6Jg6UmETWbuxyTdvGTJFzanUg1HWzPr7gSs6GLEv9VDTMiC8a5sNcGyLcHBIJo8mErrZrIssHvbT8ZUPWtyJaujKvdgazqsrad9CO3iRsZWQJ3lpvdQwucCsyjoRVoj_mXYhz3JK3wfOjLff16Gy1NLbj4gmOhBBRb8rJnUXnP7rBHs00FAk59BIpKLIPIyMgYBApDCut8V55AgXtGs4MgFFiJKbuaKxq8cdMYEVBTzDJ-S1IR5d6eiTGusD5aFlUkAs9NV_nFw");
request.AddParameter("clientId", 123);
IRestResponse<RootObject> response = client.Execute<RootObject>(request);
if (response.IsSuccessful)
{
response.Data.Write();
}
else
{
Console.WriteLine(response.ErrorMessage);
}
Solution 1:[1]
The problem is not in your code, but in the response coming from the API you are calling. I had a similar issue with an old html page whose declaration was like
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
I changed the declaration to the well-known <!DOCTYPE html>, but since the HTML response is not yours, I think your only choice is to try other ways to parse the response and/or contact the owner.
What you can try, as a slightly different approach is using System.Net.WebClient:
System.Net.WebClient oWeb = new System.Net.WebClient();
oWeb.Proxy = System.Net.WebRequest.DefaultWebProxy;
oWeb.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
string response = "";
try
{
byte[] responseBytes;
NameValueCollection coll = new NameValueCollection();
//if you need to add some headers the syntax is like: oWeb.Headers.Add("Content-Type","text/plain");
responseBytes = oWeb.UploadValues(Url, "POST", coll);
//I am using "POST" instead of "GET" but it shouldn't have conflict with your issue.
response = Encoding.UTF8.GetString(responseBytes);
//then you can parse the response as string.
ParseResponseAsString(response);
}
catch (Exception e)
{
Console.WriteLine(e);
}
Solution 2:[2]
Does the API have different possible response types?
Try adding
request.AddHeader("Accept", "application/json");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tyress |
