2

I am uploading a file on teamworking using the following link https://developer.teamwork.com/projects/questions/fileUpload-preferred

Here I am using PHP Codeigniter 4.x framework with CURL to send request and in response to the first step, I am getting Ref and URL both values. i.e. First step is working fine on my end.

Now for the second step as mentioned in the documentation as follows,

Send a PUT request to the link above, with the ‘file’ in the body of the request. Along with this, you need the following headers:

X-Amz-Acl: public-read, Content-Length: file-size Host:host-from-the-generate-link

I am passing file object

CodeIgniter\HTTP\Files\UploadedFile Object ( [path:protected] => /var/www/web116/tmp/phpDyYgjj [originalName:protected] => banner-01.png [name:protected] => banner-01.png [originalMimeType:protected] => image/png [error:protected] => 0 [hasMoved:protected] => [size:protected] => 639542 [pathName:SplFileInfo:private] => /var/www/web116/tmp/phpDyYgjj [fileName:SplFileInfo:private] => phpDyYgjj )

in request body.

Here is my code as follows:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);   // $url is value which I have gotten after first step
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                        'X-Amz-Acl: public-read',
                        'Content-Length:' . $fileSize,    // size of file
                        'Host:  tw-bucket.s3.amazonaws.com'
                    ));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['file' => $file]));  // $file is file object which I have printed above
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 $json = curl_exec($ch);
 $result = (array) json_decode($json);
 $result['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 curl_close($ch);

But in the HTTP code, I am getting 403 rather than 200.

Can anyone please help me out with this so that I can upload the file on teamwork? I am working to upload a file for a particular task on teamwork.

Thanks

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • are you sending the token? it should be passed under the Authorization header as `Bearer XXX` where `XXX` is the token. – Elad Apr 28 '21 at 14:03
  • token is not required sir, as per mentioned in their documentation – Deep Kakkar Apr 28 '21 at 14:06
  • Where did it say that you should encode the body as JSON? – CBroe Apr 28 '21 at 14:07
  • I’m guessing sending this as a “parameter” named `file` is probably wrong to begin with. PUT requests usually send _only_ the binary data of the resource that is supposed to be created, in the body, and nothing else. – CBroe Apr 28 '21 at 14:10
  • I am looking if anyone has already used this API for file upload. Meanwhile I am also trying to contact teamwork support. – Deep Kakkar Apr 28 '21 at 17:50
  • I've got the same issue with this but in Laravel and using Guzzle, do you have any reply from the Teamwork team @Deep – namnh Nov 28 '22 at 05:21
  • Hi @DeepKakkar, I also contacted them for weeks, but no reply on the issue yet, did you have any response? – namnh Dec 13 '22 at 08:57
  • naah, not yet. I am using JIRA now a days, – Deep Kakkar Dec 13 '22 at 09:12

1 Answers1

0

You can find another API to bypass this issue until the Teamwork API Support Team can fix this issue, here is what I did to make this work:

Step 1: Use the API https://apidocs.teamwork.com/docs/teamwork/86ecebd6161af-file-uploading-via-the-api-classic

To do this, you make an API Call to POST /pendingFiles.json. The actual file contents are sent via a form field called “file.”

If this is successful, you will receive a Status Code of 201 (Created) and a structure containing a reference code. { “pendingFile”: { “ref” : “tf_xxxxxxxxxxxxxxxx” } }

The ref bit is the important part and is the Pending File Handle.

Step 2: Create an actual file object using the Pending File Handle The next step is to create the actual file object in a project on your Teamwork account. To do this, you make an API Call to POST /projects/{id}/files.json where {id} is the project you want to create the file.

{
“file”: {
  “description”: “Optional string describing the file”,
  “category-id”: “ID of the category you to create the file in. Pass 0 if no category”,
  “category-name”: “String if you want to create a new category – Pass category-id=0 also”,
  “pendingFileRef”: “tf_xxxxxxxxxxxxxxxx”
  }
}

Please note the "fileId" instead of the "pendingFileRef" if you want to attach the file to a task.

This is not a pending file like the other API.

namnh
  • 503
  • 4
  • 12