401 unauthorized  Topic is solved

Discussions relating to the REST API of Jiwa 7.

401 unauthorized

Postby Riyaz » Sat Jan 12, 2019 10:18 am

Hi

Am trying to activate shipment and it returns with 401 unauthorized, am using the below code, is there an example where I can provide the authentication too? Pls advise

Dim request As WebRequest = WebRequest.Create(String.Format(My.Settings.loginAPIServer) + "Shipments/Activate/{" + _ShipmentID + "}")
request.Method = "POST"
request.ContentLength = 0

Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()

Dim jss As New JavaScriptSerializer
Dim val As apiLoginResponse = jss.Deserialize(Of apiLoginResponse)(responseFromServer)

reader.Close()
response.Close()

Note, have also tried the array option too , as below

'Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)

'request.Method = "POST"
'request.ContentType = "application/x-www-form-urlencoded"
'request.ContentLength = byteArray.Length
Riyaz
Kohai
Kohai
 
Posts: 233
Joined: Wed Dec 02, 2015 2:05 pm
Topics Solved: 2

Re: 401 unauthorized

Postby SBarnes » Sun Jan 13, 2019 7:23 am

It is far simpler to use the JsonServiceClient provided by ServiceStack as is then handles everything that needs to be passed in the headers for you once logged in below is a routine in C# that does just that which you should be able to convert to vb.net which I am assuming you are using given the code you have given.

All the vb.net DTO classes you need can get generated from the home page of the API in a number of different languages including vb.net.

Code: Select all
 bool ValidURL(string URL)
        {

            try
            {
                using (JsonServiceClient client = new JsonServiceClient(URL))
                {

                    client.Headers.Add("jiwa-stateful", "true");
                    try
                    {
                        var authResponse = client.Send<ServiceStack.AuthenticateResponse>(new ServiceStack.Authenticate()
                        {
                            provider = "credentials",
                            UserName = reader.JiwaUser,
                            Password = reader.JiwaPassword,
                            RememberMe = true
                        });
                        LogoutGetRequest logout = new LogoutGetRequest();
                        LogoutGetResponse lresp = client.Get<LogoutGetResponse>(logout);
                        return true;
                    }
                    catch (Exception ex)
                    {

                        return false;
                    }
                }
            }
            catch (Exception)
            {

                return false;
            }
        }
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1617
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: 401 unauthorized  Topic is solved

Postby Mike.Sheen » Mon Jan 14, 2019 8:59 am

Riyaz wrote:Am trying to activate shipment and it returns with 401 unauthorized, am using the below code, is there an example where I can provide the authentication too? Pls advise


You should start with the docs on Authenticating.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2440
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 755


Return to REST API

Who is online

Users browsing this forum: No registered users and 1 guest

cron