Curl Patch



Everyone has their preferred scripting language in which you are comfortable, others not so much, and this is especially true in the IT infrastructure space. I occasionally meet people who are proficient in many languages and these people tend to be programmers, annoying gits, or both (joking, it’s jealously really). Like many people who started out as windows admins I’m strong in PowerShell and I’ve learned how to work with REST APIs. But, what happens when you’re trying to use a modern REST API via swagger and the only example given is in cURL? In this post I’m going to give you 2 simple solutions to this problem.

  1. Curl Patch Command
  2. Curl Patch Php

Curl Patch Command

The first solution is a bit of a cheat and isn’t reallyconverting it, but it is useful if you are running on Windows 10. You can simplyinstall the Windows Subsystem for Linux, load the disti of your choice (I usedUbuntu) then copy the cURL example direct into the terminal/shell:

One catch is that I see many cURL examples from REST APIs don’tgive you the full script needed to run the command. The curse of “presumedknowledge” in IT will catch you out. They think that you know exactly what elseis required to get the cURL command to work, but you’re a PowerShell user. Thisisn’t your bag. So, here’s an example from the Rubrik swagger UI:

This command is missing credentials for authentication and handling of an untrusted certificate (common in IT infrastructure). What you need is­­:

If you installed the Linux subsystem and followed my example you’ve now successfully run a cURL command in Windows. But I promised you conversion to PowerShell and so that’s what we will do next.

I am using a client management software called UCRM. The API I am using is meant to patch/update but it seems to make new entries when it comes to the contacts array. Vintage 1991 signed Pretty Crimp and Curl Cabbage Patch doll KandSgiftgallery. 5 out of 5 stars (306) $ 23.50. Favorite Add to Cabbage Patch Kids Crimp and Curl Pets OldfashionedGirlUS. 5 out of 5 stars (16) $ 50.00. Favorite Add to Vintage 1992 Cabbage Patch Kids Horse Pony Crimp n Curl.

I recommend using PowerShell 6+ as it natively handles untrusted certificates and TLS 1.2. For interacting with REST APIs the equivalent of cURL in PowerShell is Invoke-RestMethod. Using this, here’s the same cURL command converted:

Let’s breakdown what I did here with the help of a diagram:

To explain:

  1. cURL natively converts credentials into a base64 string, in PowerShell you need to convert it with this command (this is the most complex difference) and embed the credentials in the header.
  2. The cURL equivalent for interacting with REST APIs in PowerShell is Invoke-RestMethod.
  3. With cURL the credentials are specified using -u which it in turns uses to construct the header, we already constructed the header in step 1 so we don’t need a credential flag in PowerShell.
  4. The method (GET ,POST, PUT etc) is signified by -X in cURL, in PowerShell its -Method.
  5. In cURL we are adding the content type to the header, in PowerShell we specify the content with -ContentType.
  6. To bypass warning on insecure certificates on the URL we are working with use –insecure in cURL, in PowerShell 6.0 we use -SkipCertificateCheck.
  7. In cURL we are just specifying the URL at the end of the command. In PowerShell you should specify it after -uri (Uniform Resource Identifier, this case the URL)
  8. The endpoint in my example doesn’t require any data so we are specifying null with -d. We could remove this section from cURL and if we were to specify this in PowerShell it would be with -Body.
Curl

Curl Patch Php

Next time anybody says PowerShell isn’t as simple as cURL for working with REST APIs you can now call them out. The only major difference is PowerShell needs a little help converting the credentials to base64, whereas its native in a shell script.

The final example I’ll give covers converting cURL to PowerShell 5.1. Admittedly this isn’t as pretty if the endpoint is using an untrusted certificate and requires TLS 1.2, both native to PowerShell 6.0+. Accommodating for both these scenarios here is the script and cURL command converted to PowerShell 5.1:

Let me know via the drift chat if you have any questions or need more examples. Happy scripting,

If you found this content useful then this small donation would help keep this blog advertisement free and the content flowing. Think about how much time it just saved you! Thanks in advance.

Related