422 - Unprocessable Entity

Hello,

I am writing a powershell script to try and create a new invoice, however
whenever I try to perform the action, I get teh following error:

422 - Unprocessable Entity

My script is as follows:

$json = @{
invoices = @(
@{
“contact”=“https://api.sandbox.freeagent.com/v2/contacts/XXX
“dated_on”=“2012-08-29T00:00:00+00:00”
“due_on”=“2013-09-28T00:00:00+00:00”
“reference”=“003”
“currency”=“GBP”
“exchange_rate”=“1.0”
“net_value”=“100.0”
“sales_tax_value”=“0.0”
“status”=“Draft”
“comments”=“An example invoice comment.”
“payment_terms_in_days”=“30”

    }
)

} | ConvertTo-Json

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

When I do a write-host $json, the format of the json looks correct:

{
“invoices”: [
{
“currency”: “GBP”,
“sales_tax_value”: “0.0”,
“due_on”: “2011-09-28T00:00:00+00:00”,
“payment_terms_in_days”: “30”,
“exchange_rate”: “1.0”,
“net_value”: “100.0”,
“contact”:
https://api.sandbox.freeagent.com/v2/contacts/2114”,
“comments”: “An example invoice comment.”,
“status”: “Draft”,
“dated_on”: “2011-08-29T00:00:00+00:00”,
“reference”: “003”
}
]
}

Are there any other pointers where I can look to see what the issue is?

Thanks

Paul

Hi Jonathan,

Exactly the same I’m afraid :frowning: I have tried incrementing the number to
something higher and removed it completely

Thanks

Paul.On Tuesday, 21 January 2014 10:46:13 UTC, Jonathan Barrett wrote:

Hi Paul,

Is the reference “003” already in use, perhaps? What happens if you omit
the reference altogether?

-J

On Tue, Jan 21, 2014 at 10:38 AM, Paul Sanders <paul.sa...@gmail.com<javascript:> wrote:

Hi Ben,

Sadly, the only response I get back is:

Exception : System.Net.WebException: The remote server
returned an error: (422) Unprocessable Entity.
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:
HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.
PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , C:\IT\Scripts\newinvoice.ps1:line
37
at , : line 1
PipelineIterationInfo : {}
PSMessageDetails :

On Monday, 20 January 2014 21:23:11 UTC, Ben wrote:

Paul,

Is there any information in the response body? There should be a list of
errors there.

Best wishes,
Ben

On Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me
specify one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -Body$json
-ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error.
Please could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I
can see that the requests are being interpreted by the server as HTML. Can
you try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.

Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_ap...@googlegroups.com <javascript:>.
To post to this group, send email to freeag...@googlegroups.com<javascript:>
.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I can
see that the requests are being interpreted by the server as HTML. Can you
try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request and
Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J

No worries :slight_smile: I have had a mare trying to format the Json response for a
while. Here is my finished code:

$token = “MyToken”
$headers = @{}
$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“ACCEPT”,“application/json”)
$invoiceURI = “https://api.sandbox.freeagent.com/v2/invoices

$json = @{
invoice =
@{
“contact”=“https://api.sandbox.freeagent.com/v2/contacts/xxxxxx
“dated_on”=“2012-08-29T00:00:00+00:00”
“due_on”=“2013-09-28T00:00:00+00:00”
“currency”=“GBP”
“net_value”=“100.0”
“sales_tax_value”=“20.0”
“status”=“Draft”
“comments”=“An example invoice comment.”
“payment_terms_in_days”=“30”
“project”=“https://api.sandbox.freeagent.com/v2/projects/xxx
“invoice_items” = @(
@{
“description”=“Test InvoiceItem”
“item_type”=“Hours”
“price”=“5.0”
“quantity”=“10.0”
}
)
}
} | ConvertTo-Json -Depth 12

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -Body$json
-ContentType “application/json”

The main issue I had was the “invoice_item” array within the hashtable. To
solve, ‘convertto-json’ has a -Depth flag that digs deeper into the
variable to convert it.

I am going to write a powershell module for doing stuff with FreeAgent, so
will see if I can find a way to retrieve and parse the response body. I
will keep you all updated.

Once again, thanks very much for your help!On Tuesday, 21 January 2014 15:28:56 UTC, Jonathan Barrett wrote:

No worries Paul: I feel a bit daft for not noticing that straight off :slight_smile:

That said, when I punted your original query (modified to match a contact
on our test account), I was getting a bunch of validation errors sent
back in the response body. They weren’t amazingly helpful (they were
complaining about a missing contact), but I was seeing them. You might want
to dig into your framework to see how you’d go about accessing the response
body even in the case of a “non-successful” response, as it might prove
helpful for debugging in the future.

Cheers,

-J

On Tue, Jan 21, 2014 at 2:54 PM, Paul Sanders <paul.sa...@gmail.com<javascript:> wrote:

Hi Jonathan,

Good spot, thanks :smiley: It is now working, I needed to do:
$json = @{
invoice =
@{
“contact”="
https://api.sandbox.freeagent.com/v2/contacts/21114"

        "dated_on"="2012-08-29T00:00:00+00:00"
        "due_on"="2013-09-28T00:00:00+00:00"

        "currency"="GBP"
        "net_value"="100.0"
        "sales_tax_value"="20.0"

        "status"="Draft"
        "comments"="An example invoice comment."
        "payment_terms_in_days"="30"

“project”=“https://api.sandbox.freeagent.com/v2/projects/462
@{
}
} | ConvertTo-Json

On Tuesday, 21 January 2014 12:19:54 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry, I just took a closer look at your JSON: you’re posting an array
of invoices instead of a single invoice. Try posting:

{ “invoice”: {
“currency”: …
}}

and let me know how you get on.

-J

On Tue, Jan 21, 2014 at 11:11 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Exactly the same I’m afraid :frowning: I have tried incrementing the number to
something higher and removed it completely

Thanks

Paul.

On Tuesday, 21 January 2014 10:46:13 UTC, Jonathan Barrett wrote:

Hi Paul,

Is the reference “003” already in use, perhaps? What happens if you
omit the reference altogether?

-J

On Tue, Jan 21, 2014 at 10:38 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Ben,

Sadly, the only response I get back is:

Exception : System.Net.WebException: The remote server
returned an error: (422) Unprocessable Entity.
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:
HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.
PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , C:\IT\Scripts\newinvoice.
ps1: line 37
at , : line 1
PipelineIterationInfo : {}
PSMessageDetails :

On Monday, 20 January 2014 21:23:11 UTC, Ben wrote:

Paul,

Is there any information in the response body? There should be a
list of errors there.

Best wishes,
Ben

On Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me
specify one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -
Body $json -ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error.
Please could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get
on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our
logs, I can see that the requests are being interpreted by the server as
HTML. Can you try this again, but make sure you specify the correct Accept
and Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it,
send an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.

Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it,
send an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011 begin_of_the_skype_highlighting +44 (0)131 447
0011 FREE end_of_the_skype_highlighting

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_ap...@googlegroups.com <javascript:>.
To post to this group, send email to freeag...@googlegroups.com<javascript:>
.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774

Paul,

Is there any information in the response body? There should be a list of
errors there.

Best wishes,
BenOn Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders paul.sanders87@gmail.comwrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me specify
one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -Body$json
-ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error. Please
could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I can
see that the requests are being interpreted by the server as HTML. Can you
try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_api+unsubscribe@googlegroups.com.
To post to this group, send email to freeagent_api@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

PaulOn Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I can
see that the requests are being interpreted by the server as HTML. Can you
try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J

Hi Paul,

Sorry, I just took a closer look at your JSON: you’re posting an array of
invoices instead of a single invoice. Try posting:

{ “invoice”: {
“currency”: …
}}

and let me know how you get on.

-JOn Tue, Jan 21, 2014 at 11:11 AM, Paul Sanders paul.sanders87@gmail.comwrote:

Hi Jonathan,

Exactly the same I’m afraid :frowning: I have tried incrementing the number to
something higher and removed it completely

Thanks

Paul.

On Tuesday, 21 January 2014 10:46:13 UTC, Jonathan Barrett wrote:

Hi Paul,

Is the reference “003” already in use, perhaps? What happens if you omit
the reference altogether?

-J

On Tue, Jan 21, 2014 at 10:38 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Ben,

Sadly, the only response I get back is:

Exception : System.Net.WebException: The remote server
returned an error: (422) Unprocessable Entity.
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt
pWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.
PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , C:\IT\Scripts\newinvoice.ps1:line
37
at , : line 1
PipelineIterationInfo : {}
PSMessageDetails :

On Monday, 20 January 2014 21:23:11 UTC, Ben wrote:

Paul,

Is there any information in the response body? There should be a list
of errors there.

Best wishes,
Ben

On Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me
specify one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -
Body $json -ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error.
Please could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I
can see that the requests are being interpreted by the server as HTML. Can
you try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.

Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_api+unsubscribe@googlegroups.com.
To post to this group, send email to freeagent_api@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me specify
one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -Body$json
-ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from working.
Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error. Please
could you check the logs to see if anything else is missing?

Thanks

PaulOn Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I can
see that the requests are being interpreted by the server as HTML. Can you
try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J

Hi Ben,

Sadly, the only response I get back is:

Exception : System.Net.WebException: The remote server returned
an error: (422) Unprocessable Entity.
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:
HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.
Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , C:\IT\Scripts\newinvoice.ps1:line
37
at , : line 1
PipelineIterationInfo : {}
PSMessageDetails :On Monday, 20 January 2014 21:23:11 UTC, Ben wrote:

Paul,

Is there any information in the response body? There should be a list of
errors there.

Best wishes,
Ben

On Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders <paul.sa...@gmail.com<javascript:> wrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me specify
one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -Body$json
-ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error. Please
could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I
can see that the requests are being interpreted by the server as HTML. Can
you try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_ap...@googlegroups.com <javascript:>.
To post to this group, send email to freeag...@googlegroups.com<javascript:>
.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774

Hi Paul,

Is the reference “003” already in use, perhaps? What happens if you omit
the reference altogether?

-JOn Tue, Jan 21, 2014 at 10:38 AM, Paul Sanders paul.sanders87@gmail.comwrote:

Hi Ben,

Sadly, the only response I get back is:

Exception : System.Net.WebException: The remote server
returned an error: (422) Unprocessable Entity.
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:
HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell
.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , C:\IT\Scripts\newinvoice.ps1:line
37
at , : line 1
PipelineIterationInfo : {}
PSMessageDetails :

On Monday, 20 January 2014 21:23:11 UTC, Ben wrote:

Paul,

Is there any information in the response body? There should be a list of
errors there.

Best wishes,
Ben

On Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me
specify one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -Body$json
-ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error. Please
could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs, I
can see that the requests are being interpreted by the server as HTML. Can
you try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.

Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_api+unsubscribe@googlegroups.com.
To post to this group, send email to freeagent_api@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774

Hi Jonathan,

Good spot, thanks :smiley: It is now working, I needed to do:
$json = @{
invoice =
@{
“contact”=“https://api.sandbox.freeagent.com/v2/contacts/21114
“dated_on”=“2012-08-29T00:00:00+00:00”
“due_on”=“2013-09-28T00:00:00+00:00”
“currency”=“GBP”
“net_value”=“100.0”
“sales_tax_value”=“20.0”
“status”=“Draft”
“comments”=“An example invoice comment.”
“payment_terms_in_days”=“30”
“project”=“https://api.sandbox.freeagent.com/v2/projects/462
@{
}
} | ConvertTo-JsonOn Tuesday, 21 January 2014 12:19:54 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry, I just took a closer look at your JSON: you’re posting an array of
invoices instead of a single invoice. Try posting:

{ “invoice”: {
“currency”: …
}}

and let me know how you get on.

-J

On Tue, Jan 21, 2014 at 11:11 AM, Paul Sanders <paul.sa...@gmail.com<javascript:> wrote:

Hi Jonathan,

Exactly the same I’m afraid :frowning: I have tried incrementing the number to
something higher and removed it completely

Thanks

Paul.

On Tuesday, 21 January 2014 10:46:13 UTC, Jonathan Barrett wrote:

Hi Paul,

Is the reference “003” already in use, perhaps? What happens if you omit
the reference altogether?

-J

On Tue, Jan 21, 2014 at 10:38 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Ben,

Sadly, the only response I get back is:

Exception : System.Net.WebException: The remote server
returned an error: (422) Unprocessable Entity.
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:
HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.
PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , C:\IT\Scripts\newinvoice.ps1:line
37
at , : line 1
PipelineIterationInfo : {}
PSMessageDetails :

On Monday, 20 January 2014 21:23:11 UTC, Ben wrote:

Paul,

Is there any information in the response body? There should be a list
of errors there.

Best wishes,
Ben

On Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me
specify one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -
Body $json -ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error.
Please could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs,
I can see that the requests are being interpreted by the server as HTML.
Can you try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it,
send an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.

Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_ap...@googlegroups.com <javascript:>.
To post to this group, send email to freeag...@googlegroups.com<javascript:>
.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774

No worries Paul: I feel a bit daft for not noticing that straight off :slight_smile:

That said, when I punted your original query (modified to match a contact
on our test account), I was getting a bunch of validation errors sent
back in the response body. They weren’t amazingly helpful (they were
complaining about a missing contact), but I was seeing them. You might want
to dig into your framework to see how you’d go about accessing the response
body even in the case of a “non-successful” response, as it might prove
helpful for debugging in the future.

Cheers,

-JOn Tue, Jan 21, 2014 at 2:54 PM, Paul Sanders paul.sanders87@gmail.comwrote:

Hi Jonathan,

Good spot, thanks :smiley: It is now working, I needed to do:
$json = @{
invoice =
@{
“contact”="https://api.sandbox.freeagent.com/v2/contacts/21114
"

        "dated_on"="2012-08-29T00:00:00+00:00"
        "due_on"="2013-09-28T00:00:00+00:00"

        "currency"="GBP"
        "net_value"="100.0"
        "sales_tax_value"="20.0"

        "status"="Draft"
        "comments"="An example invoice comment."
        "payment_terms_in_days"="30"

“project”=“https://api.sandbox.freeagent.com/v2/projects/462
@{
}
} | ConvertTo-Json

On Tuesday, 21 January 2014 12:19:54 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry, I just took a closer look at your JSON: you’re posting an array of
invoices instead of a single invoice. Try posting:

{ “invoice”: {
“currency”: …
}}

and let me know how you get on.

-J

On Tue, Jan 21, 2014 at 11:11 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Exactly the same I’m afraid :frowning: I have tried incrementing the number to
something higher and removed it completely

Thanks

Paul.

On Tuesday, 21 January 2014 10:46:13 UTC, Jonathan Barrett wrote:

Hi Paul,

Is the reference “003” already in use, perhaps? What happens if you
omit the reference altogether?

-J

On Tue, Jan 21, 2014 at 10:38 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Ben,

Sadly, the only response I get back is:

Exception : System.Net.WebException: The remote server
returned an error: (422) Unprocessable Entity.
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.
WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:
HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.
PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , C:\IT\Scripts\newinvoice.ps1
: line 37
at , : line 1
PipelineIterationInfo : {}
PSMessageDetails :

On Monday, 20 January 2014 21:23:11 UTC, Ben wrote:

Paul,

Is there any information in the response body? There should be a list
of errors there.

Best wishes,
Ben

On Mon, Jan 20, 2014 at 5:53 AM, Paul Sanders paul.sa...@gmail.comwrote:

Hi Jonathan,

Ok, I have had a play and have noticed the following:

The invoke-restmethod cmdlet has a -ContentType flag that lets me
specify one part of the header:

invoke-restmethod -Uri $invoiceURI -Method Post -Headers $headers -
Body $json -ContentType “application/json”

However, I still need to add the Accept Header using my Dict:

$headers.add(“AUTHORIZATION”,“Bearer $token”)
$headers.add(“accept”,“application/json”)

There was a known bug within Powershell 3.0 that stopped this from
working. Updating to Version 4 allowed me to include the accept header.

Now my powershell bug is fixed, I am still getting the 422 error.
Please could you check the logs to see if anything else is missing?

Thanks

Paul

On Monday, 20 January 2014 13:11:45 UTC, Paul Sanders wrote:

Hi Jonathan,

Thanks for that. That makes perfect sense :slight_smile:

I will give this a try a little later and let you know how I get
on.

Thanks

Paul

On Monday, 20 January 2014 10:41:39 UTC, Jonathan Barrett wrote:

Hi Paul,

Sorry you’re having trouble here. Taking a quick look at our logs,
I can see that the requests are being interpreted by the server as HTML.
Can you try this again, but make sure you specify the correct Accept and
Content-Type headers in your request. This page in the docs on Request
and Response formatshttps://dev.freeagent.com/docs/introduction#about-request-and-response-formatsshould help.

Cheers,

-J


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it,
send an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.

Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to freeagent_ap...@googlegroups.com.
To post to this group, send email to freeag...@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified
www.freeagent.com

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_api+unsubscribe@googlegroups.com.
To post to this group, send email to freeagent_api@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Barrett, Senior Engineer

FreeAgent: Accounting software, simplified

+44 (0)131 447 0011

Follow us on Twitter: @freeagent https://twitter.com/freeagent

FreeAgent Central Ltd, 40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774