REST API Request Using Invoke-RestMethod

%3CLINGO-SUB%20id%3D%22lingo-sub-1454340%22%20slang%3D%22en-US%22%3EREST%20API%20Request%20Using%20Invoke-RestMethod%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1454340%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20having%20a%20devil%20of%20time%20formatting%20a%20request%20to%20a%20REST%20API.%26nbsp%3B%20I've%20tried%20twenty%20different%20ways%20of%20passing%20the%20request%20information%20in%20headers%5Cbody%20using%20Invoke-RestMethod.%20Here's%20what%20the%20request%20needs%20to%20look%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPUT%20%2Fapi%2Fv1%2Ffiles%2FcliScript%2Fincremental%3CBR%20%2F%3EContent-Type%3A%20application%2Foctet-stream%3CBR%20%2F%3E%3CSHOW%20system%3D%22%22%20uptime%3D%22%22%3E%3C%2FSHOW%3E%3C%2FP%3E%3CP%3E%3CSHOW%20system%3D%22%22%20version%3D%22%22%3E%3C%2FSHOW%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can't%20figure%20out%20how%20to%20pass%20these%20SHOW%20SYSTEM%20commands.%20Has%20anyone%20tackled%20something%20like%20this%20before%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1454340%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1454579%22%20slang%3D%22en-US%22%3ERe%3A%20REST%20API%20Request%20Using%20Invoke-RestMethod%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1454579%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F351954%22%20target%3D%22_blank%22%3E%40Danny_B855%3C%2FA%3EHere's%20the%20last%20thing%20I%20tried%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24headers%20%3D%20New-Object%20%22System.Collections.Generic.Dictionary%5B%5BString%5D%2C%5BString%5D%5D%22%3CBR%20%2F%3E%24headers.Add(%22Authorization%22%2C%20'Basic%20'%20%2B%20%24encodedCreds)%3CBR%20%2F%3E%24headers.Add(%22Content-Type%22%2C%20%22application%2Foctet-stream%60r%60n%3CSHOW%20system%3D%22%22%20uptime%3D%22%22%3E%60r%60n%3CSHOW%20system%3D%22%22%20version%3D%22%22%3E%22)%3C%2FSHOW%3E%3C%2FSHOW%3E%3C%2FP%3E%3CP%3E%24Response%20%3D%20Invoke-RestMethod%20%22%3CA%20href%3D%22https%3A%2F%2F1.2.3.4%2Fapi%2Fv1%2Ffiles%2FcliScript%2Fincremental%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2F1.2.3.4%2Fapi%2Fv1%2Ffiles%2FcliScript%2Fincremental%3C%2FA%3E%22%20-Headers%20%24headers%20-Method%20Put%3C%2FP%3E%3CP%3EError%3A%3CBR%20%2F%3EInvoke-RestMethod%20%3A%20Specified%20value%20has%20invalid%20CRLF%20characters.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1455672%22%20slang%3D%22en-US%22%3ERe%3A%20REST%20API%20Request%20Using%20Invoke-RestMethod%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1455672%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20seem%20to%20be%20passing%20them%20as%20header%20values%2C%20try%20it%20with%20the%20body%20of%20the%20request%20instead.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I'm having a devil of time formatting a request to a REST API.  I've tried twenty different ways of passing the request information in headers\body using Invoke-RestMethod. Here's what the request needs to look like:

 

PUT /api/v1/files/cliScript/incremental
Content-Type: application/octet-stream
<show system uptime>

<show system version>

 

I can't figure out how to pass these SHOW SYSTEM commands. Has anyone tackled something like this before?

 

2 Replies

@Danny_B855Here's the last thing I tried:

 

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", 'Basic ' + $encodedCreds)
$headers.Add("Content-Type", "application/octet-stream`r`n<show system uptime>`r`n<show system version>")

$Response = Invoke-RestMethod "https://1.2.3.4/api/v1/files/cliScript/incremental" -Headers $headers -Method Put

Error:
Invoke-RestMethod : Specified value has invalid CRLF characters.

You seem to be passing them as header values, try it with the body of the request instead.