Home > OS >  CURL example to Invoke-RestMethod
CURL example to Invoke-RestMethod

Time:09-19

I am trying to convert curl command to powershell Invoke-RestMethod

Command from MobileIron (in case someone will google that topic)

curl --user <username>:<password> --header 'Content-Type: application/json' --request PUT 'https://
<mobileiron_core>/api/v2/devices/wakeup?adminDeviceSpaceId=1' --data '{"deviceUuids": ["af0cea8bf56c-
49f5-b615-76cc34936b2f"], "note": "something"}'

Ok, my code (based on https://gist.github.com/rayterrill/e83a1bd877547eccfd59b656e7f91b48#file-mobileiron-addremovelabels-ps1 )

$url = $mi_server   "/api/v2/devices/wakeup?adminDeviceSpaceId=1"
$contentType = "application/json"      

$headers = @{
    Authorization=("Basic {0}" -f $regase64AuthInfo)
}

$data = @{        
    note = "test";
    deviceUuids = $guid
};

$json = $data | ConvertTo-Json;
Invoke-RestMethod -Method PUT -Uri $url -ContentType $contentType -Headers $headers -Body $data;

But i am getting 400 error - so,as i understand - something wrong with --data , but what ?

CodePudding user response:

MobileIron 400 is a general error: 400 Bad request, Meaning the request was invalid.
The accompanying error message (That follows the the error code) explains the reason.
Can you post the rest of the error message?

You cannot use --data or -Body with a PUT request.

You must make a query string for the data.
The query string must be url encoded.

Because it is HTTPS transfer you may need -k (--insecure)

The PUT request may work, I believe MobileIron prefers GET, There is no real difference between the two. I tested both.

I tried (I did my testing with PHP)
A PUT Request
Request Headers

 Content-Type: application/json
 Accept: application/json

POST DATA was the same as the Query String
I checked with and without the POST DATA and no change.
I also sent the POST DATA with no query string and it was if not data was sent.

URL and Query

 $query = urlencode('{"deviceUuids": ["af0cea8bf56c-49f5-b615-76cc34936b2f"], "note": "something"}');
 $url =  https://<domain>/api/v2/devices/wakeup? . $query

URL was changed to my server to an app that returns the request header, and parameters.
Which returned this on the server:

Request Headers:

Content-Type: application/json
Accept: application/json
Accept-Encoding: deflate, gzip, br
Expect: 100-continue
Host: mydomain.com
Transfer-Encoding: chunked

The server GET and server REQUEST values

$_GET
array (
  '{"deviceUuids":_' => 
  array (
    '"af0cea8bf56c-49f5-b615-76cc34936b2f"' => '',
  ),
)
$_REQUEST
array (
  '{"deviceUuids":_' => 
  array (
    '"af0cea8bf56c-49f5-b615-76cc34936b2f"' => '',
  ),
)

The Request Query

$_SERVER['QUERY_STRING'])  (url decoded)
{"deviceUuids": ["af0cea8bf56c-49f5-b615-76cc34936b2f"], "note": "something"}

Notice above the JSON query string was cut off.
But was in the arguments

argv array (url decoded)    
0 = {"deviceUuids":
1 = ["af0cea8bf56c-49f5-b615-76cc34936b2f"],
2 = "note":
3 = "something"}

I removed the Content-Type: application/json from the request header and there was no change.

I changed the PUT to GET, no change

When a I started sending the Content=Type: application/json
With a POST request, and no Query String, the POS Data was ONLY in the BODY of the request.

And here are some example requesst from some MobileIron documentation:
The first request was a default GET not a PUT.

curl -u admin_username:admin_password -kv "https://URL_to_your_mobileiron_cloud_environment/api/v1/[email protected]&[email protected]&accountSource=Local" -d ''

curl GET -kv -u user:password "https://[mobileiron_cloud]/api/v1/device/57d9903c-aadf-4b40-aef3-e1f24302f180/customattributes”

curl -X POST -kv -u user:password -H 'Content-Type: application/json' -d '{ "attrs" : { "tlark" : [ "1" ] } } ' "https://[mobileiron_cloud]/api/v1/device/22000/customattributes"

curl -X POST -kv -u user:password -H 'Content-Type: application/json' -d '{ "attrs" : { "tlark" : [ "1" ] } } ' "https://[mobileiron_cloud]/api/v1/device/8bcc4cee-dca9-476d-8710-9bb1e738ade9/customattributes"

CodePudding user response:

   $param_pam_pam = @{
        Method      = 'PUT'
        Uri         = "$url"
        ContentType = "application/json"
        Headers     = @{authorization = ("Basic {0}" -f $regase64AuthInfo) }
        Body        = @{
                        deviceUuids = @($guid)
                        note = "force check-in from $ma"
                        } | ConvertTo-Json
    }

    $reg = Invoke-RestMethod @param_pam_pam 
  • Related