POST using XML is OK, JSON is giving an error

Hello,

I am trying to create a contact using the v2 API. If I try using XML
format then it works fine, if I switch to JSON then I get an error. As
far as I can see the requests are the same apart from the Accept and
Content-Type headers.

Can anybody see why the XML works but the JSON doesn’t?

Thank you!

Chris

XML FORMAT

<?php $params = ' AA AA '; $header = array( 'Authorization: Bearer MY-TOKEN-GOES-HERE', 'Accept: application/xml', 'Content-Type: application/xml' ); $ch = curl_init('https://api.freeagent.com/v2/contacts'); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_USERAGENT, "My App"); $res = curl_exec($ch); curl_close($ch); echo $res; ?>

JSON FORMAT

<?php $params = array(); $params['first_name'] = 'XX First Name'; $params['last_name'] = 'XX Last Name'; $params = json_encode($params); $header = array( 'Authorization: Bearer MY-TOKEN-GOES-HERE', 'Accept: application/json', 'Content-Type: application/json' ); $ch = curl_init('https://api.freeagent.com/v2/contacts'); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_USERAGENT, "My App"); $res = curl_exec($ch); curl_close($ch); echo $res; ?>

JSON RESPONSE

{“errors”:[{“message”:“first_name can’t be blank”},
{“message”:“last_name can’t be blank”},{“message”:“organisation_name
can’t be blank”}]}

Hi Chris,

Nic is correct about the JSON format being the problem. See the docs for
examples: https://dev.freeagent.com/docs/contacts

Kind regards,

GraemeOn 29 April 2012 07:38, Nic Wise nicw@fastchicken.co.nz wrote:

Hi Chris

I suspect your json and XML dont match. You XML is

Contact
First name
Last name
/contact

And the JSON would be

{
First name:
Last name:
}

Ie missing the contact wrapper.

On 28 Apr 2012, at 19:38, Chris geek@p4d.co wrote:

Hello,

I am trying to create a contact using the v2 API. If I try using XML
format then it works fine, if I switch to JSON then I get an error. As
far as I can see the requests are the same apart from the Accept and
Content-Type headers.

Can anybody see why the XML works but the JSON doesn’t?

Thank you!

Chris

XML FORMAT

<?php $params = ' AA AA '; $header = array( 'Authorization: Bearer MY-TOKEN-GOES-HERE', 'Accept: application/xml', 'Content-Type: application/xml' ); $ch = curl_init('https://api.freeagent.com/v2/contacts'); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_USERAGENT, "My App"); $res = curl_exec($ch); curl_close($ch); echo $res; ?>

JSON FORMAT

<?php $params = array(); $params['first_name'] = 'XX First Name'; $params['last_name'] = 'XX Last Name'; $params = json_encode($params); $header = array( 'Authorization: Bearer MY-TOKEN-GOES-HERE', 'Accept: application/json', 'Content-Type: application/json' ); $ch = curl_init('https://api.freeagent.com/v2/contacts'); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_USERAGENT, "My App"); $res = curl_exec($ch); curl_close($ch); echo $res; ?>

JSON RESPONSE

{“errors”:[{“message”:“first_name can’t be blank”},
{“message”:“last_name can’t be blank”},{“message”:“organisation_name
can’t be blank”}]}


You received this message because you are subscribed to the Google
Groups “FreeAgent API” group.
To post to this group, send email to freeagent_api@googlegroups.com.
To unsubscribe from this group, send email to
freeagent_api+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/freeagent_api?hl=en.


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To post to this group, send email to freeagent_api@googlegroups.com.
To unsubscribe from this group, send email to
freeagent_api+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/freeagent_api?hl=en.

Graeme Boyd
Senior Software Engineer
FreeAgent Central Ltd
40 Torphichen Street, Edinburgh EH3 8JB
Registered in Scotland SC316774

Hi Chris

I suspect your json and XML dont match. You XML is

Contact
First name
Last name
/contact

And the JSON would be

{
First name:
Last name:
}

Ie missing the contact wrapper.On 28 Apr 2012, at 19:38, Chris geek@p4d.co wrote:

Hello,

I am trying to create a contact using the v2 API. If I try using XML
format then it works fine, if I switch to JSON then I get an error. As
far as I can see the requests are the same apart from the Accept and
Content-Type headers.

Can anybody see why the XML works but the JSON doesn’t?

Thank you!

Chris

XML FORMAT

<?php $params = ' AA AA '; $header = array( 'Authorization: Bearer MY-TOKEN-GOES-HERE', 'Accept: application/xml', 'Content-Type: application/xml' ); $ch = curl_init('https://api.freeagent.com/v2/contacts'); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_USERAGENT, "My App"); $res = curl_exec($ch); curl_close($ch); echo $res; ?>

JSON FORMAT

<?php $params = array(); $params['first_name'] = 'XX First Name'; $params['last_name'] = 'XX Last Name'; $params = json_encode($params); $header = array( 'Authorization: Bearer MY-TOKEN-GOES-HERE', 'Accept: application/json', 'Content-Type: application/json' ); $ch = curl_init('https://api.freeagent.com/v2/contacts'); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_USERAGENT, "My App"); $res = curl_exec($ch); curl_close($ch); echo $res; ?>

JSON RESPONSE

{“errors”:[{“message”:“first_name can’t be blank”},
{“message”:“last_name can’t be blank”},{“message”:“organisation_name
can’t be blank”}]}


You received this message because you are subscribed to the Google Groups “FreeAgent API” group.
To post to this group, send email to freeagent_api@googlegroups.com.
To unsubscribe from this group, send email to freeagent_api+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/freeagent_api?hl=en.