0

I am trying to implement a ReST request in PowerShell.

Request 1:

$response = Invoke-RestMethod "my-custom-url" -Headers $headers -Method POST -Body $json -ContentType "application/json" -OutFile output.json -SessionVariable sv

Reponse from Request 1:

{
  "@type": "user",
  "id": "00000703000000000010",
  "orgId": "000007",
  "name": "xxx@gmail.com",
 }

I need to save the value of "id" & add it to the header of next request. How can I achieve that?

----Update--Adding the WriteHost $response

@{@type=user; id=00000703000000000010; orgId=000007; name=xxx; createTime=2014-08-27T12:12:21.000Z; updateTime=2016-02-27
T00:40:13.000Z; createdBy=xxx@gmail.com; updatedBy=xxx; sfUsername=ppanigrahi; firstName=Pravakar; lastName=P
anigrahi; password=********; phone=9008433201; emails=xxx@gmail.com; timezone=IST; serverUrl=http://localhost:16006/saas; spiUrl
=https://localhost:8080/; icSessionId=QpXZEkqYEflJRY7h; securityQuestion=MOTHER_MAIDEN_NAME; forceChangePassword=False; uuid=C1bL60uIQqyRaF3
nXCHFkA; roles=System.Object[]; usergroups=System.Object[]}
live2learn
  • 861
  • 2
  • 9
  • 16

2 Answers2

1

Since you're using Invoke-RestMethod, the response will be an object containing the properties with the data and you can access the id from the $Response.id.

I'm not sure if you want to try to pass the "Id" as a header value or as a cookie (as you're referring to your session variable also; where you'd normally persist e.g. cookies etc).

If all you want is to pass a custom-header containing the field "Id" you can do the following before the next request:

$headers.Add("Id",$response.id);

I'm assuming you've defined $headers on beforehand here. Else you can create it directly with $headers = @{"Id"=$response.id};.

Then on the next service call just reuse your $headers variable as you allready did.

If what you really are trying to achieve is to create a cookie and add this one to your session (variable) so you don't have to repeat yourself for every subsequent call; you should create a cookie and add it doing the following:

$c=New-Object System.Net.Cookie;
$c.Name="Id";
$c.Path="/"
$c.Value = $response.Id;
# Make sure domain and path is matching your backend-server
$c.Domain = "foobar.com"; 
# Add cookie to your websession
$sv.Cookies.Add($c);

This will create a cookie and add it to your websession (if $sv is in your session). If you don't have a webrequest session ($sv in your case), you can create this on before hand using $sv = New-Object Microsoft.PowerShell.Commands.WebRequestSession;

Harald F.
  • 4,505
  • 24
  • 29
  • tried the same but unsuccessful. It'g giving 403 forbidden error. I also tried just to print the vale of Id using - Write-Host $response.id. But this doesn't print anything. – live2learn Mar 02 '16 at 07:31
  • Can you print an example. If you write-host $response.id and it's empty; then your server doesn't return the JSON you wrote. If your first call gives an 403; then you don't get any response. We cannot help you if you don't provide more information. What version of powershell? What does $response write out if you Write-host? – Harald F. Mar 02 '16 at 07:35
  • powershell version 5.0. Modifying the question to provide the $response – live2learn Mar 02 '16 at 07:39
  • Now I am able to get the details as you mentioned. Thanks a ton for helping out!! All I did is remove the -Outfile parameter & -SessionVariable parameter. Unable to understand why it wouldn't work with the above 2 parameters. – live2learn Mar 02 '16 at 07:47
  • 1
    I missed that you're using -Outfile. If you use outfile, the result are just saved to file and not passed along the pipeline. If you want to save the result to a file AND return the result use the -OutFile "Filename.something" with -PassThru. – Harald F. Mar 02 '16 at 07:49
  • Thanks! It cleared my concept!! Can you also please let me know what the $c.Path refers to while adding a cookie in the above explanation? – live2learn Mar 02 '16 at 07:55
  • 1
    The path refers to the path for where to accept cookies; e.g. if you have multiple apps running on the same domain and want to restrict the cookies using the path. Read more info e.g. here: https://en.wikipedia.org/wiki/HTTP_cookie (domain and paths). – Harald F. Mar 02 '16 at 08:13
0

$response.id should contain the value you need, as Powershell automatically converts a json response to a PSCustomObject. You should then be able to add that value to your $headers variable via $headers.Add("HeaderName",$response.id) or create a new hashtable with the appropriate header name, $request2Headers = @{ "HeaderName" = $response.id }.

Cobster
  • 1,243
  • 10
  • 18