Uploading File

20 views
Skip to first unread message

Gregory Shaw

unread,
Feb 7, 2014, 4:36:12 AM2/7/14
to assembla...@googlegroups.com
Hello

I have tried to use this code to upload a file using curl / php and  I get a HTTP 422 error.

Not sure where the error could be.


$headers    = array('X-Api-Key: e14cf5','X-Api-Secret: 5b6aac6df0d639c14b7b555d','Content-Type multipart/form-data');
     
 $p = '@' . realpath('C:/Users/ci1gsx/Downloads/AcYear.pdf ');   
$ch = curl_init();
echo($ch);
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $p,
    CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
print("HTTP status: " . $http_status);;

Any help would be appreciated.

Thank
Greg

Maxim Cretu

unread,
Feb 7, 2014, 4:44:36 AM2/7/14
to assembla...@googlegroups.com
Hello Gregory,

It will be nice to see the response body of the request, a 422 status comes always with the description of the error. This looks like a validation error and the response body will help you understand.

Gregory Shaw

unread,
Feb 7, 2014, 4:52:58 AM2/7/14
to assembla...@googlegroups.com
Hello Maxim

When I did this code:

$x = json_decode($response,true);

var_dump($x);

I got:

Resource id #2HTTP status: 422
array (size=1)
  'errors' => 
    array (size=1)
      'name' => 
        array (size=1)
          0 => string 'can't be blank' (length=14)

Any ideas would be great. Thanks for helping

Greg

Stanislav Kolotinskiy

unread,
Feb 7, 2014, 5:14:39 AM2/7/14
to assembla...@googlegroups.com
Hi Gregory,

your request is malformed. The very least that you should pass is document[file] param, which will contain a path to the file you want to upload. Please check http://api-doc.assembla.com/content/ref/documents_create.html for some examples. From what I'm seeing, you would want to send something like:
$p = array('document[file]', '@' . realpath('C:/Users/ci1gsx/Downloads/AcYear.pdf'))
Looks like you're missing a colon between Content-Type and multipart/form-data in your headers declaration. Please recheck that.

Regards,
Stanislav
--
You received this message because you are subscribed to the Google Groups "Assembla API Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to assembla-api-d...@googlegroups.com.
To post to this group, send email to assembla...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages