Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

convertto-json issues

$
0
0

Evening All,

I was hoping you guys & gals could point me in the right direction for a script I am writing?

I use an online accountancy package that I need to automatically created invoices to. I am using powershell and system center to link it into various other solutions that I use. The script I have written looks like:

$token = "mytoke"
$headers = @{}
$headers.add("AUTHORIZATION","Bearer $token")
$invoiceURI = "https://provider.com/v2/invoices"
$body = @{contact="https://provider.com/v2/contacts/21114";dated_on="2001-12-12T00:00:00+00:00";due_on="2001-12-12T00:00:00+00:00";reference="004";currency="GBP";net_value="100.00";status="Draft";payment_terms_in_days="30"}
$jsonprebody = @{invoice=$body}
$jsonbody = $body | convertto-json
$completebody = $jsonprebody | convertto-json

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -Body $body

When I run this script, I get:

invoke-restmethod : The remote server returned an error: (422) Unprocessable Entity.

When I do a wrote-host on my $jsonbody, it displays as:

"invoice":  {"net_value":  "100.00","contact":  "provider.com/v2/contacts/21114","reference":  "004","due_on":  "2001-12-12T00:00:00+00:00","status":  "Draft","currency":  "GBP","dated_on":  "2001-12-12T00:00:00+00:00","payment_terms_in_days":  "30"
                }

I can see the issue, as I need the body request to look like:

{ "invoices": [
  {"url":"https://provider.com/v2/invoices/1","contact":"https://provider.com/v2/contacts/2","dated_on":"2011-08-29T00:00:00+00:00","due_on":"2011-09-28T00:00:00+00:00","reference":"001","currency":"GBP","exchange_rate":"1.0","net_value":"0.0","sales_tax_value":"0.0","status":"Draft","comments":"An example invoice comment.""omit_header":false,"payment_terms_in_days":30,"ec_status":"Non-EC"
  }
]}

Where am I going wrong? I need to build the body so that I have { "invoice": [ $body]

Thanks,

Paul




Kind Regards, Paul Sanders | MCSE PC | MCITP EA, VA | MCTS SCCM/SCOM | My Blog: http://tameyourcloud.com


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>