Is it possible to attach an uploaded file to http request? - php

I have a form that submits a file to an endpoint. In theory that endpoint takes the file and attaches it to another post request that goes to the API.
I was trying to figure out how to do it with Laravel and read a few things that suggest it's not possible? That the only solution would be save it in a public place and submit the link to the API. Anyone able to shed some light on this? Below is the code that I thought would work but does not send the file.
$this->client->post('/api/endpoint', ['form_params' => ['file' => $request->file('file')->getRealPath()]])
Client being the Guzzle client.

Yes it is. Guzzle Docs
$client->request('POST', '/post', [
'multipart' => [
[
'name' => 'foo',
'contents' => 'data',
'headers' => ['X-Baz' => 'bar']
],
[
'name' => 'baz',
'contents' => fopen('/path/to/file', 'r')
],
[
'name' => 'qux',
'contents' => fopen('/path/to/file', 'r'),
'filename' => 'custom_filename.txt'
],
]
]);

It seems fairly uncommon for APIs to handle file uploads as this is generally something a person does. If i were to do something like this I would first read the contents of the file and base64 encode it. Then send the normal Http POST request with the base64 encoded file content as a string. On the API, you could then decode it and do whatever you need to o with the file.
Dealing with base64 in PHP is fairly simple http://php.net/manual/en/function.base64-encode.php

Related

Possibly recoverable warning exit code from extractor: -536870935 when translating Revit source file

I have been following along with the Translate a Revit File, Generating Room and Space Information tutorial. Right now I'm stuck on task 3 trying to translate a Revit source file to SVF2.
Using the provided Revit file and the following request POST https://developer.api.autodesk.com/modelderivative/v2/regions/eu/designdata/job:
// Headers
[
'Authorization' => ...,
'Content-Type' => 'application/json',
'x-ads-force' => true,
]
// Body
[
"input" => [
"urn" => "<some urn>",
"compressedUrn" => false
],
"output" => [
"destination" => [
"region" => "EMEA"
],
"formats" => [
[
"type" => "svf2",
"views" => [
"2d",
"3d"
],
"advanced" => [
"generateMasterViews" => true
]
]
]
]
]
I always get the following messages:
Revit-UnsupportedFileType
The file is not a Revit file or is not a supported version. TranslationWorker-RecoverableInternalFailure
Possibly recoverable warning exit code from extractor: -536870935
I hope someone can tell what is wrong with the POST request. I found a similar question but the answer doesn't seem apply to this issue.
I actually tried this same tutorial myself and it works perfectly on my end. As long as you follow every step, you are supposed to get the desired result.
If you're running into an error in the third task, it shows there might be something you didn't do correctly in Task 2.
There are a few steps you should check in Task 2 that Task 3 is dependent upon. Please check the following:
Make sure your file is fully uploaded. Look at "Upload the file" section in Task 2 of the tutorial.
You must inform OSS (Object Storage Service) that the upload operation is complete. Look at "Finalize Upload" section in Task 2.
These actions should ensure that your file is fully uploaded and ready to be translated to SVF2
NOTE: When doing all these processes, make sure your Access Token is valid as it only remains valid for one hour. If the token expires, you must obtain a fresh token by sending an authenticate request to Forge once again.
When asking the question I was using the following code to upload the file to Autodesk using Laravel's http client:
$response = Http::attach( 'file', $file->getContent(), $file->getClientOriginalName() )
->put( $signed_url );
This did upload the files and everything worked for zipped models. But as explained in the question plain source files like .ipt or .rvt did not translate.
I guess Laravel is adding something to that request that somehow corrupts files for Autodesk. Using the following request:
$response = Http::withHeaders( [
'Content-Disposition' => 'attachment; filename='.$file->getClientOriginalName(),
'Content-Length' => $file->getSize(),
] )
->send(
'PUT',
$signed_url,
[
'body' => $file->getContent(),
]
);
it does work. I guess using send, sends a "raw"/ binary request.
In Symfony it would look something like:
$response = HttpClient::create()->request(
'PUT',
$signed_url,
[
'body' => $file->getContent(),
'headers' => [
'Content-Disposition' => 'attachment; filename='.$file->getClientOriginalName(),
'Content-Length' => $file->getSize()
]
]
);
I'm not familiar with Lavarel so I might not offer much help on that part.
However, you could try and use another framework like Nodejs or even .NET to try and see if you can achieve your desired results.
You can follow this tutorial that will help you get started in creating your application on top of the Autodesk platform using either Nodejs or .NET framework: https://forge-tutorials.autodesk.io/
You will get to use the Model Derivative API and you can then try and convert the model as intended.

How to Commit Mp3 Files to Folders in Gitlab through API

I am trying to commit a file to Gitlab through the API.
The code had been working for over 6 months but now has stopped working, nothing in the code had changed. The file is commited to Gitlab, however it is corrupted.
I have went through the Guzzle documentation and everything looks correct, and I have done the same for the Gitlab documentation about commits.
I am now using the Laravel Illuminate/Http class to send the commits but the same thing is still happening. I am able to commit to Gitlab, but the file is not formatted correctly.
$response = Http::withHeaders([
'PRIVATE-TOKEN' => $gitlab_token,
])
->post('https://gitlab.com/api/v4/projects/.../repository/commits/', [
'branch' => 'test-branch',
'commit_message' => 'Updated audio file test.mp3',
'actions' => array(array(
'action' => 'update',
'file_path' => 'filePath/../.mp3',
'content' => base64_encode($var)
)),
]);
If I do not encode the contents of the file to base 64 I get the error:
Malformed UTF-8 characters, possibly incorrectly encoded in file
Has anything changed on the API side that has effected how files are processed for committing? Has anyone else found a solution?
I think you have two problems; first there's no content type specified. The post request will be sent just as multipart/form-data with a file attachment, or application/x-www-url-encoded without. This API is expecting JSON data.
Second, there is a parameter in the commit endpoint to specify file encoding. It defaults to "text" but you are sending a base64-encoded file.
I'm not familiar enough with the Gitlab API to say for sure, but your file_path property looks strange as well; shouldn't it just be ".mp3" to be put into that directory?
Try this:
$response = Http::withHeaders([
'PRIVATE-TOKEN' => $gitlab_token,
'Content-Type' => 'application/json',
])
->post('https://gitlab.com/api/v4/projects/.../repository/commits/', [
'branch' => 'test-branch',
'commit_message' => 'Updated audio file test.mp3',
'actions' => [
[
'action' => 'update',
'file_path' => '.mp3',
'content' => base64_encode($var),
'encoding' => 'base64',
]
],
]);

Guzzle Post Request send file with additional param

I have problem with Guzzle sending post file. In API i have methoda with input like files (for file), name and isPublic. I dont know how to send these two additional param (filename and isPublic, maybe I understand multipart params in wrong way?
My example
$response = $client->request('POST', 'http://www.example.com/files/post', [
'multipart' => [
[
'name' => 'files' - its input name in api for files?
'contents' => fopen('path/to/file', 'r')
'filename' => 'csv_header.csv'
//Should I add additional param name and isPublic in this array, or somewhere else?
]]]
Please for advice. Thank You :)

How to copy a remote file with Guzzle in WebDAV?

I am trying to understand how to copy a file on a remote WebDAV server to another location on that same server by using Guzzle. I currently have
$client->request('COPY', 'file1.txt', [
'Destination' => 'file2.txt',
'Overwrite' => 'T',
]);
This method is giving me a 400 response
Client error: 'COPY http://example.com/remote.php/dav/files/admin/file1.txt' resulted in a '400 Bad Request' response:
file1.txt does exist and it's not a permissions issue.
I'm following some docs* and trying to guess my way to get it working as I can't find any examples online.
Can anyone let me know what I need to change?
*eg https://docs.nextcloud.com/server/12/developer_manual/client_apis/WebDAV/index.html
I found the answer. The Destination and Overwrite parameters need to be sent in the headers.
$headers = [
'Destination' => 'file2.txt',
'Overwrite' => 'T',
]);
$client->request('COPY', 'file1.txt', [
'headers' => $headers,
]);
There seems to be a real lack of documentation on WebDAV.

Upload image from Library Photos iOS to cakephp

I'm trying to upload an UIImage that I took from the library photos on iOS to cakephp. The problem I'm really a beginner on cake so I have no idea how it works. I added the plugin: cakemanager/cakephp-utils to cake then added an upload behavior then in my table I initialized my attribute just like this
$this->addBehavior('Utils.Uploadable', [
'control_mfront' => [
'fields' => [
'filePath' => 'control_mfront',
],
'field' => 'id',
'path' => '{ROOT}{DS}{WEBROOT}{DS}img{DS}{model}{DS}{field}{DS}',
'fileName' => 'control_mfront'.date('YmdHis').'.{extension}','removeFileOnUpdate' => false
]
]);
and on my Controller on the post I wrote this :
$this->request->data['model_mfront']['name'] = 'model_mfront';
until now on the website it works fine but for iOS i didn't understand how should I post the image . is it on NSData format !?
and what next ?!
thanks
You can convert your imagedata(NSData) in base64 string and then send it to server as post request.

Categories