Sunday, June 23, 2019

Wednesday, June 19, 2019

How to send a message to a Microsoft Teams Channel with Curl


  1. Create a channel
  2. Add the connector "Incoming Webhook"
  3. Get the connector URL




set URL=https://outlook.office.com/webhook/6ec7c854-ac26-441b-89db-22222222@11111111-f841-496f-8a84-eff218b6ee27/IncomingWebhook/44444444444444444444/1f15fc1e-aea1-474a-a7b3-33333333333333

set DATA={""@context"": ""http://schema.org/extensions"",""@type"": ""MessageCard"",""text"": ""Hello World Yvon""}

curl.exe -XPOST -H "Content-type: application/json" -d "%DATA%" %URL%

Sunday, June 2, 2019

C# async method returning multiple parameters

Overview


C# 7 now supports function and method that return multiple values.
The reasons why I like it are
  • Output parameter require more code to write and do not make the code easier to read
  • The other solution is to create a specific class that contains all the values. It is clean but require more code. 
  • The Python language had this feature for a long time based on the concept of Tuple. 
  • A previous version of C# introduced the concept of Tuple, but all the names had to be Item1, Item2.
In C# 7 there is a new Tuples concept. For more about it see link Tuples with C# 7.0.

Async Method


The more tricky question is how is this supposed to work with async method.

public async Task < ( DonationDTO donationDTO, string messageId ) > DequeueAsync()
{
    var m = await _queueManager.DequeueAsync();
    if(m == null)
    {
        return ( null, null );
    }
    else
    {
        base.TrackNewItem();
        var donationDTO = DonationDTO.FromJSON(m.AsString);

        return ( donationDTO, m.Id );
    }
}

// How to call the method - Solution 1
var result = await donationQueue.DequeueAsync();
var donationDTO = result.donationDTO;
var messageId = result.messageId;

// How to call the method - Solution 2
(DonationDTO donationDTO, string messageId) = await donationQueue.DequeueAsync();

Monday, May 27, 2019

Issue with Curl posting some json data on windows

Overview

If you are testing your api with Curl, you should be aware of this issue, escaping double quote in JSON data.

Problem 

Most of the documentation and blog on the InterWeb, mention the following format which I suppose works in Linux
curl -H "Content-Type: application/json; charset=utf-8" -X POST 
      --data '{"Name":"foo foo", "Id":1}' https://localhost:44399/api/values

curl -H "Content-Type: application/json; charset=utf-8" -X POST 
      --data "{\"Name\":\"foo foo\", \"Id\":1}" https://localhost:44399/api/values



These formats do not work in a Windows MS-DOS prompt.

Solution

 In a Windows MS-DOS prompt, use the following
# Solution 1, by escaping the " the right way
curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST 
      --data "{""Name"":""foo foo"", ""Id"":1}" https://localhost:44399/api/values

# Solution 2, by piping the json data
echo {"Name":"foo foo", "Id":1} | curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST -d @- https://localhost:44399/api/values

# Solution 3, store the json data into a file
curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST -d @data.json https://localhost:44399/api/values


 In a Windows PowerShell prompt, use the following

# With PowerShell make sure to call curl.exe
curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST 
      --data "{`"`"Name`"`":`"`"foo foo`"`", `"`"Id`"`":1}" https://localhost:44399/api/values


# Another sample with PowerShell
cls
$contentType = "Content-Type: application/json; charset=utf-8"
$data ='{ `Guid`:`cd7af44d-db7b-4d4c-9157-052ce5f50836`,`FirstName`:`Sonny`,`LastName`:`Haking`,`Email`:`shaking0@theguardian.com`,`Gender`:`Male`,`Phone`:`310-632-6062`,`IpAddress`:`138.27.230.192`,`Country`:`Indonesia`,`Amount`:`$91.37`,`CC_Number`:`4026367644878790`,`CC_ExpMonth`:12,`CC_ExpYear`:2022,`CC_SecCode`:233}'
$data = $data.replace("``","""""").replace("""","``""")
$url = "https://localhost:44399/api/Donation"
$command = "curl.exe -H `"$contentType`" -X POST --data `"$data`" $url "
Write-Host $command
Write-Host ""
Invoke-Expression $command



This issue can make it very difficult to troubleshoot asp.net [FromBody] auto mapping feature.
It cost me 3 hours of my life.

public class DTO
{
    public string Name { get; set; }
    public int Id { get; set; }
}

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    [HttpPost]
    public IActionResult Post([FromBody] DTO dto)
    {
        Console.WriteLine($"POST dto name {dto.Name}");
        return Ok();
    }
}

Friday, May 10, 2019

How to transpile a js file from the command line with babel

const f = () => { // < Should be transpiled
    const a = { a:1 };
    const b = { ...a }; // < Should be transpiled

    console.log('f running '+ JSON.stringify(b));
};

f();

Command Line
npx babel a.js --out-dir ./tmp-fred --ignore spec.js,test.js
The file .babelrc.js should be executed. With Babel v7 it is recommended to use the file babel.config.js, to apply to the transpilation rules to every folder including folder from node_modules, if some libraries need to be transpiled.

Links


Monday, May 6, 2019

How to reset the password of the ftp user for an Azure App Service

In the left section click on the following

  • Deployment 
  • Deployment Center
  • FTP Tils
  • DashBoard Button
  • User Credentials Tab

Thursday, April 25, 2019

javascript destructuring in function parameters

Reminder that Javascript destructuring is more powerful that it looks like

Sample

const theKey = 'key';

function f(
 {
  a: {
   b,
   // 'theDefaultValue'  is the default value in case theKey value is not 
   // defined in the object passed to the function
   [theKey] : theKeyValue = 'theDefaultValue' 
  }
 }
 ) {
  console.log(`b:${b}`);
  console.log(`theKeyValue:${theKeyValue}`);
}

f( 
 { a: {
   b:1, 
   'key': 'myValue'
  }
 }
);

f( 
 { a: {
   b:1 
  }
 }
);

Output

b:1
theKeyValue:myValue
b:1
theKeyValue:theDefaultValue