Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Get_attachment gives JsonReaderException at Line 0, Position 0 every time

#1

Thanks for the updated API and attachment goodies :slight_smile:

However, every time I try to use the “get_attachment” I get an error:

UNHANDLED EXCEPTION: NEWTONSOFT.JSON.JSONREADEREXCEPTION: UNEXPECTED CHARACTER ENCOUNTERED WHILE PARSING VALUE: ?. PATH ‘’, LINE 0, POSITION 0.
AT NEWTONSOFT.JSON.JSONTEXTREADER.PARSEVALUE()
AT NEWTONSOFT.JSON.JSONTEXTREADER.READ()
AT NEWTONSOFT.JSON.LINQ.JOBJECT.LOAD(JSONREADER READER, JSONLOADSETTINGS SETTINGS)
AT NEWTONSOFT.JSON.LINQ.JOBJECT.PARSE(STRING JSON, JSONLOADSETTINGS SETTINGS)
AT GUROCK.TESTRAIL.APICLIENT.SENDREQUEST(STRING METHOD, STRING URI, OBJECT DATA)
AT GUROCK.TESTRAIL.APICLIENT.SENDGET(STRING URI)
AT PRODUCETESTREPORT.PRODUCETESTREPORT.MAIN(STRING ARGS) IN C:…\SOURCE\REPOS\PRODUCETESTREPORT\PRODUCETESTREPORT\PRODUCETESTPROJECT.CS:LINE 162

The lines of code in question are:

            // Base URL for Project
            string dwnURL = "https://novsqskal.testrail.io/";
            // Creates new instance of APIClient attaching to TestRail instance
            APIClient trDwnAttClient = new APIClient ( dwnURL );
            // User Name and Password supplied.
            trDwnAttClient.User = userName;
            trDwnAttClient.Password = trPwd;
            // Call to client asking it to GET attachment 1078 (This is an image)
            JObject trAttStr = (JObject) trDwnAttClient.SendGet ( "get_attachment/1078" );

Using POSTMAN or cUrl and it works just fine, I get an image.

Using the api through c# it does not work.

Any ideas?

Thanks,
Ivor

#2

How do All,

Finally figured out what I was doing wrong and wrote myself a wee routine based on the code in the APIClient.cs file which is part of the API binding.

I should have treated the response in it’s raw form instead of trying to handle it through the strict JSon/API “get_attachment” method, which will always return JSon data (or will try to).

This code will execute the request and save the response to a PNG file:

    /// <summary>
    /// Routine used to grab the image attached to a test or a result, the full call is specified in the first variable.
    /// </summary>
    /// <param name="trFullAPICall"></param>
    /// <param name="trAttFile"></param>
    /// <returns>Status of the response/exception that may have occurred </returns>
    private static String TRGetSaveAttachment( string trFullAPICall, string trAttFile )
    {
        string strResponseValue = String.Empty;

        try
        {
            // Standard stuff lifted from the APIClient.cs file
            HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create ( trFullAPICall );
            request.Method = "GET";
            request.ContentType = "application/json";
            string auth = Convert.ToBase64String (
                Encoding.ASCII.GetBytes (
                    String.Format (
                        "{0}:{1}" ,
                        userName ,
                        trPwd
                    )
                )
            );
            request.Headers.Add ( "Authorization" , "Basic " + auth );

            using ( HttpWebResponse response = ( HttpWebResponse ) request.GetResponse ( ) )
            {
                if ( response.StatusCode != HttpStatusCode.OK )
                {
                    throw new ApplicationException ( "Error occured: " + response.StatusCode.ToString ( ) +" - "+ response.StatusDescription.ToString ( ) );
                }
                else
                {
                    Image trAttImage = Image.FromStream ( response.GetResponseStream ( ) , false , true );
                    trAttImage.Save ( trAttFile , ImageFormat.Png );
                }

                return response.StatusCode.ToString ( );
            }
        }
        catch ( WebException exception )
        {
            using ( var reader = new StreamReader ( exception.Response.GetResponseStream ( ) ) )
            {
                strResponseValue = reader.ReadToEnd();
            }
            return strResponseValue.ToString( );
        }
    }