3

This is the structure I have:

Server 1: Form to upload files and save them in internal folder.
Server 2: Just a Repository with files.

What I need:

A user from Server 1 upload a file, then, I need to invoke a Rest WS to copy that file from Server 1 to server 2.

to aproach to this solution I was trying to encode the file and send the string coded to the RestService, then, decode the string and create a new file in server2. This actually works for files with 2 or 3MB, but dont know if is the best way to do it and dont know if support bigger files (20 to 30MB).

Avhelsing
  • 81
  • 2
  • 7
  • why dont you just transfer your original file from one server to another using REST. Why you encode that. encoding is good when you are dealing with image file and general purpose file on web. You can do like if you upload the file on server 1, after completion you can trigger a POST request to send your data along with file to another server. Second solution is, you can write a backend cron job which check the new uploads and call second server's REST endpoint making POST request to that REST endpoint. Same situation i had and If you want like how i did then I can post detail. – BetaDev Mar 24 '17 at 14:45
  • This may be a stupid question, but, how can I actually attach the file with the data to the rest service? – Avhelsing Mar 24 '17 at 14:51
  • You dont have to attach, how you submit files using HTML form to a PHP script? that same with rest – BetaDev Mar 24 '17 at 15:06

1 Answers1

1

You can upload file to a REST endpoint as you can do with PHP. Remember what you do when you use an HTML form and PHP. You simply post your form and get the file on server using your PHP code. Its same with REST endpoints (APIs) nothing difference.
First of all, in your HTML form you specify the action, the action attribute represents one REST endpoint (API) where you are going to submit your Data and files (Uploads).
For more clarification when you do simple HTML form POST operation please try to inspect your post request on your browser's console where you can find what you are submitting and how the form data along with the uploaded files are being submitted to action URL of the form same thing applies to REST Endpoints (APIs), nothing special there, but you need to define your form programmatically (If not HTML form) so that you can submit your request.

In your case for example make a POST request to a REST endpoint as

POST /api/v1/myfileupload HTTP/1.1
Host: 123.12.12.12:3000
Authentication: Bearer some-token //there might be token based authentication
Content-Type: image/jpeg
Content-Length: 284

raw image content

You need to be clear about REST, REST is nothing, In REST we just define our virtual web browser.

For more detail: Please see: Link

REST endpoint is a URL just like as other web site URL. See this reference, how you can post your file to another server using PHP Curl. Posting ti another server and posting to REST endpoint is same thing. Post Files

BetaDev
  • 4,516
  • 3
  • 21
  • 47
  • I was trying to follow your example, (img- deleted) – Avhelsing Mar 24 '17 at 15:43
  • you dont have anything names as file, or simply you are accessing as file but there is nothing you have submitted as name file. file as a POST field. In your image file i can not see the upper one. – BetaDev Mar 24 '17 at 15:46
  • I'm trying to acces the $_FILE php var. Same as the example. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_REQUEST["fileName"]); – Avhelsing Mar 24 '17 at 15:49
  • first of all thats not `$_FILE` that should be `$_FILES` – BetaDev Mar 24 '17 at 15:52
  • your error is associated with ....[file], arror says this file index not defined. You are passing your values in different name and accessing with different name. – BetaDev Mar 24 '17 at 15:54