Monday, 27 February 2017

[Newtonsoft] Checking for null valued keys in parsed JObject

Using Newtonsoft's Json package, when we need to parse a string to get a JObject, we can do it as follows:

JObject o = JObject.Parse(serializedJsonString);
This is of course the case when you don't have a model class for the Json response coming in. For keys that can have a null value, checking for null values as follows is intuitive.

o["key"] == null

However, it is also incorrect. Due to the way in which null values are stored in the parsed object, the proper way of checking for null valued keys is as follows:

o["key"].Type == JTokenType.Null
Of course, you should do both checks and the former check should be done earlier to ensure that the key exists before checking whether the value is null.

Friday, 7 October 2016

Web request with HTTP authentication from Powershell

Web requests can be made from Powershell with HTTP authentication in the following 2 ways:

1. Using credential objects
$username = "foo"
$password = "bar" | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)
$res = Invoke-WebRequest http://localhost:3000 -Credential $cred

2. Adding appropriate header
$acctname = "foo"
$password = "bar"
$params = @{uri = 'http://localhost:3000';
                   Method = 'Get'; #(or POST, or whatever)
                   Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)"));
           } #end headers hash table
   } #end $params hash table
$var = curl @params

Curl above is same as Invoke-WebRequest because on Windows, by default curl is just an alias to Invoke-WebRequest.

A JSON request with a body can be made as follows:

$res = Invoke-WebRequest http://localhost:3000/stores -Credential $cred -Method POST -Headers @{"Content-Type"="application/json"} -Body (ConvertTo-Json @{"Key"="Value"})

Saturday, 13 August 2016

Frustrating experience with AWS Beanstalk

I have used Azure's Paas services before. For a friend's website, I was trying out AWS's Paas offering today. The experience is nothing less than just frustrating. I have a fully functioning rails application. It is working fine locally. However, when I tried to upload the application using the beanstalk command line, I am seeing the following error.

ERROR: [Instance: i-002808929ac3e534b] Command failed on instance. Return code: 1 Output: (TRUNCATED)...SyntaxError: Invalid CSS after "": expected selector or at-rule, was "{"
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `load'
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `
Tasks: TOP => assets:precompile
(See full trace by running task with --trace).
Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/ failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

Now the complete log is not available here. You need to log into the instance and check it. However, as the lines show, it is a problem with asset pre-compilation. I tried running asset compilation locally; but I do not see this error. Interestingly, the instance created by the CLI did not allow permission for SSH. It's acceptable source was set to some other IP. It seems to me they launched their product in a hurry.

Wednesday, 22 June 2016


Robocopy is remarkably different from other copy commands. It is often used in msbuild projects but there are a bunch of caveats which would appear non-intuitive to a first time user. The reason people consider using it is because it allows retries and after parameterised wait intervals.

The default syntax of robocopy takes folders as parameters and copies source folder to destination. To copy a file, the syntax is the following:

robocopy C:\source C:\destination filename.ext

Notice that filename is not part of the source here. Also, because of this limitation a file can not be copied to another location with a different name. Due to this, in msbuild projects, you need to copy the file using robocopy (assuming you use robocopy) and then rename it.

Monday, 20 June 2016

Detailed changes of a stash

I have been using git for a few years now. Stashing is one of the very frequent operations I do on git. So, on a number of occasions, I find myself with the need of looking into what the stash contains (Perhaps, I should branch more and stash less). The following command, which shows the detailed changes in a particular stash, does come in handy in those situations.

 git stash show -p stash@{1}

Tuesday, 14 June 2016

Bypassing SSL in WebRequests

Using C#, there are two prevalent ways of making HTTP requests:
- using HttpWebRequest
- using HttpClient

When you have a cancellation token that you want to use, only the later provides the facility for that. So, there are scenarios for each method to be adopted is what I am getting at.

While accessing HTTPS sites whose SSL certificates are not trusted, we manually allow access in case of the browser. The same can b achieved via code using the following methods.

1. Setting ServerCertificateValidationCallback for the request.
This method only for the first method of making HTTP requests.

// provide a custom callback
request.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(SSLValidationDelegate);

// define the callback to allow always
private static bool SSLValidationDelegate(Object o, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors errors){
    return true;

2. Setting ServerCertificateValidationCallback for the ServicePointManager class.
This method essentially does the same but instead of doing it at the request level it does that for all.

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
HttpClient client = new HttpClient();
CancellationTokenSource cts = new CancellationTokenSource();
string url = "https://localhost/some/path";
HttpResponseMessage response = client.GetAsync(url, cts.Token).Result;

So when a cancellation needs to be used,we have to take the second route.

Thursday, 19 May 2016

Last Shutdown time on Windows

Checking the last shut down time on Linux is as simple as reading through logs. On Windows, it is not as straight forward though. Recently, I needed to find the last shutdown time of my Windows desktop. I knew System events is what I should be looking at but searching through the logs is not as easy as a regular expression. After some reading, I found that I can filter the log with the following parameters to search for the shutdown events.

Event source: USER32
Event Id: 1074

Thursday, 28 April 2016

Pull from a shared Windows machine

As a distributed version control system, one of the benefits of using git is that you can pull from anyone's repo just as you would do from a server. With Git's support for SSH, it is very easy to do it on Linux. Two Windows machines don't talk SSH to each other though. So, I was not sure  how to pull from my repo on one Windows machine to another. It turned out equally easy though (may be easier).

git pull file:////<machine_name>E$/path/to/repo/root

Drives like E: and C: get replaced by E$ and C$ respectively. As both of my machines were on the same domain, it was easy. Sharing might come into picture for machines on different domains.

Sunday, 3 April 2016

Adhaar card woes

I have had my Adhaar card for a few years now; much before the government started incentivising it. However, then the card did not have a phone number information with it. Now, when I want to use it, it seems the new portal needs phone authentication. I guess whoever implemented that feature never thought of backward compatibility. Now, even though I have an adhaar card, I can't use it.

I guess I am part of collateral damage in government's march for progress. I talked to adhaar customer care and they told me to go to the Adhaar office at Banjara hills, Hyderabad and meet the manager of the institution, who should be able to update my phone number. When I went there, the manager was a difficult person to find as the place was jam-packed. He told me that he can't do it in the afternoon and asked me to come during the morning some other day.

All this while I wasn't aware that there is an adhaar office at Madhapur. The customer care associate (read moron) had asked me for my PIN code and she could not find which office is nearer to me. So, this time I went to the nearer office. The manager of the first office had told me that the office will be open on weekends too. However, when I went to the second office, it was closed on weekends.

I guess this is the bottom line of "digital India". You still need to make multiple trips to government agencies to get the simplest of things done.