Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

json post request to send a file to an api in laravel

i want to send a file from one server (website A) to another one (website B).i am using an api to achieve this.my api post file works very well and it able to upload the file.i want to use a json post request to send the file to website B.i have integrated the jquery code at system A where it should be called after the file has been uploaded in server A but the call does not work and the function doesn't execute.i have done some research and still i haven't found an answer on how to call the upload function that will upload the file in system B.should i make a new function or should add it to the upload function that uploads in system A.i want to achieve this after the function uploads the image in system A another one uploads it to system B.i have tried this but it doesnt respond.

  // Sending AJAX request and upload file
async function uploadData(formdata) {
$.ajax({
    url: '{{route('product.savePicture')}}',
    type: 'post',
    data: formdata,
    contentType: false,
    processData: false,
    dataType: 'json',
    success: function (response) {
        addThumbnail(response);
    }
});

// push file to system b
var form = new Formdata();
    form.append("file", fileInput.files[0], $filename);
    form.append("systemid",$product_details->systemid);
    form.append("id",$product_details->id);

    var settings = {
    "url": "http://systemb/api/push_h2image",
    "method": "POST",
    "timeout": 0,
    "headers": {
        "Content-Type": "multipart/form-data;boundary=<calculated when request is sent>"
    },
    "processData": false,
    "mimeType": "multipart/form-data",
    "contentType": false,
    "data": form
    };

    $.ajax(settings).done(function (response) {
    console.log(response);
    });
0 likes
10 replies
Snapey's avatar
Snapey
Best Answer
Level 122

you need a route and a controller on system B that works as if it is receiving a file via a form submission

You will also need to make it unauthenticated and excluded from csrf protection

Ideally you would send the file directly from server A not the client of server A so as to not expose the api.

So, send from client to Server A to Server B

stephen waweru's avatar

@Snapey yes already i have the route and controller that receives the data which are unauthenticated.what i want achieve is after the first function saves the file in website A then another one proceeds to save it in website B

Sinnbeck's avatar

@stephen waweru I completely agree with @snapey

Sending data from one backend to another.. Pretty simple.

Sending data from javascript on one page to the backend of the other.. Way harder, as you are not sending it from server 1, but from the clients browser.

Please or to participate in this conversation.