Page 1 of 1

401 unauthorized

PostPosted: Sat Jan 12, 2019 10:18 am
by Riyaz
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

Re: 401 unauthorized

PostPosted: Sun Jan 13, 2019 7:23 am
by SBarnes
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;
            }
        }

Re: 401 unauthorized  Topic is solved

PostPosted: Mon Jan 14, 2019 8:59 am
by Mike.Sheen
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