Call Frappe REST API on C# with Authentication?

I need to call Rest API Frappe on Application by C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

public enum HttpVerb
{
    GET,
    POST,
    PUT,
    DELETE
}

namespace CallRestAPI
{
    public class RestClient
    {
        public string EndPoint { get; set; }
        public HttpVerb Method { get; set; }
        public string ContentType { get; set; }
        public string PostData { get; set; }

        public string login()
        {
            string endPoint = @"http://192.168.2.237/api/method/login?usr=Administrator&pwd=admin";
            var client = new RestClient(endPoint);
            var json = client.MakeRequest();
            return json.ToString();
        }
        public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
        {
            string authInfo = userName + ":" + userPassword;
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            req.Headers["Authorization"] = "Basic " + authInfo;
        }
        public RestClient()
        {
            EndPoint = "";
            Method = HttpVerb.GET;
            ContentType = "text/xml";
            PostData = "";
        }
        public RestClient(string endpoint)
        {
            EndPoint = endpoint;
            Method = HttpVerb.GET;
            ContentType = "text/xml";
            PostData = "";
        }
        public RestClient(string endpoint, HttpVerb method)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "text/xml";
            PostData = "";
        }

        public RestClient(string endpoint, HttpVerb method, string postData)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "text/xml";
            PostData = postData;
        }


        public string MakeRequest()
        {
            return MakeRequest("");
        }

        public string MakeRequest(string parameters)
        {
            var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);

            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("Administrator" + ":" + "admin")));
            //SetBasicAuthHeader(request, "Administrator", "admin");
            request.Credentials = new NetworkCredential("Administrator", "admin");
            request.Method = Method.ToString();
            request.ContentLength = 0;
            request.ContentType = ContentType;

            if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
            {
                var encoding = new UTF8Encoding();
                var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
                request.ContentLength = bytes.Length;

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                    throw new ApplicationException(message);
                }

                // grab the response
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }

                return responseValue;
            }
        }
    }
}

Method I call

private void btnCallRestAPI_Click(object sender, EventArgs e)
        {
            RestClient rest = new RestClient();
            string login = rest.login();

            rest.EndPoint = @"http://192.168.2.237/api/resource/Customer"; ;
           
            var json = rest.MakeRequest();
        }

I have a ERROR β€œ403”.

The remote server returned an error: (403) Forbidden.

How can I using Authentication ?
I find in the internet. They answer me using

request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("Administrator" + ":" + "admin")));

But It’s Error.
Who do have experience call Rest API on C# ?
Can you help me ?

1 Like

No experience with C#

But maybe you are not passing correct session id via cookies in your get request.

Thank rmehata. I am trying with session id via cookies . Error fixed

2 Likes