Using cURL to upload POST data with files

Using Curl To Upload Post Data With Files

We Are Going To Discuss About Using cURL to upload POST data with files. So lets Start this Article.

You want to use the -F possibility:
-F/--form <title=content material> Specify HTTP multipart POST data (H)

Try this:

curl 
  -F "userid=1" 
  -F "filecomment=This is an image file" 
  -F "[email protected]/home/user1/Desktop/test.jpg" 
  localhost/uploader.php

Catching the person id as path variable (beneficial):

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "[email protected]" http://mysuperserver/media/1234/upload/

Catching the person id as a part of the shape:

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "[email protected];userid=1234" http://mysuperserver/media/upload/

or:

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "[email protected]" -F "userid=1234" http://mysuperserver/media/upload/

Here is my answer, I’ve been studying a variety of posts they usually have been actually useful. Finally I wrote some code for small files, with cURL and PHP that I feel its actually helpful.

public perform publishFile()
{    
        $file_url = "test.txt";  //right here is the file route, on this case is on identical listing however you may set URL too like "http://examplewebsite.com/test.txt"
        $eol = "rn"; //default line-break for mime sort
        $BOUNDARY = md5(time()); //random boundaryid, is a separator for every param on my publish curl perform
        $BODY=""; //init my curl physique
        $BODY.= '--'.$BOUNDARY. $eol; //begin param header
        $BODY .= 'Content-Disposition: form-data; title="sometext"' . $eol . $eol; // final Content with 2 $eol, on this case is only one content material.
        $BODY .= "Some Data" . $eol;//param data on this case is an easy publish data and 1 $eol for the top of the data
        $BODY.= '--'.$BOUNDARY. $eol; // begin 2nd param,
        $BODY.= 'Content-Disposition: form-data; title="somefile"; filename="test.txt"'. $eol ; //first Content data for publish file, keep in mind you solely put 1 when you're going to add extra Contents, and a pair of on the final, to shut the Content Instance
        $BODY.= 'Content-Type: utility/octet-stream' . $eol; //Same earlier than row
        $BODY.= 'Content-Transfer-Encoding: base64' . $eol . $eol; // we put the final Content and a pair of $eol,
        $BODY.= chunk_split(base64_encode(file_get_contents($file_url))) . $eol; // we write the Base64 File Content and the $eol to end the data,
        $BODY.= '--'.$BOUNDARY .'--' . $eol. $eol; // we shut the param and the publish width "--" and a pair of $eol on the finish of our boundary header.



        $ch = curl_init(); //init curl
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                         'X_PARAM_TOKEN : 71e2cb8b-42b7-4bf0-b2e8-53fbd2f578f9' //customized header for my api validation you will get it from $_SERVER["HTTP_X_PARAM_TOKEN"] variable
                         ,"Content-Type: multipart/form-data; boundary=".$BOUNDARY) //setting our mime sort for make it work on $_FILE variable
                    );
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/1.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'); //setting our person agent
        curl_setopt($ch, CURLOPT_URL, "api.endpoint.post"); //setting our api publish url
        curl_setopt($ch, CURLOPT_COOKIEJAR, $BOUNDARY.'.txt'); //saving cookies simply in case we would like
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // name return content material
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); navigate the endpoint
        curl_setopt($ch, CURLOPT_POST, true); //set as publish
        curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // set our $BODY 


        $response = curl_exec($ch); // begin curl navigation

     print_r($response); //print response

}

With this we ought to be get on the “api.endpoint.post” the next vars posted. You can simply check with this script, and you ought to be obtain this debugs on the perform publishFile() on the final row.

print_r($response); //print response

public perform getPostFile()
{

    echo "nn_SERVERn";
    echo "<pre>";
    print_r($_SERVER['HTTP_X_PARAM_TOKEN']);
    echo "/<pre>";
    echo "_POSTn";
    echo "<pre>";
    print_r($_POST['sometext']);
    echo "/<pre>";
    echo "_FILESn";
    echo "<pre>";
    print_r($_FILEST['somefile']);
    echo "/<pre>";
}

It ought to work effectively, they could be higher options however this works and is actually useful to perceive how the Boundary and multipart/from-data mime works on PHP and cURL library.

READ :  Step-by-step debugging with IPython

in case you are importing binary file resembling csv, use beneath format to upload file

curl -X POST 
    'http://localhost:8080/employees' 
    -H 'authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyIsInR5cGUiOiJhY2Nlc3MifQ.eyJ1c2VySWQiOjEsImFjY291bnRJZCI6MSwiaWF0IjoxNTExMzMwMzg5LCJleHAiOjE1MTM5MjIzODksImF1ZCI6Imh0dHBzOi8veW91cmRvbWFpbi5jb20iLCJpc3MiOiJmZWF0aGVycyIsInN1YiI6ImFub255bW91cyJ9.HWk7qJ0uK6SEi8qSeeB6-TGslDlZOTpG51U6kVi8nYc' 
    -H 'content-type: utility/x-www-form-urlencoded' 
    --data-binary '@/house/limitless/Downloads/iRoute Masters - Workers.csv'

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *