简体   繁体   中英

Send CSV file to jersey rest API in java using php CURL request?

I would like to call Rest API which is in Jersey java. The code of rest API is :

@POST
@Path("/csv1")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile1(@FormDataParam("type") int type ,@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail)
{
}

I want to call it from CURL request using ajax in php. How can I do so?

If you drop the AJAX requirement, this is how you send a file with cURL from PHP to the /csv1 API at example.com.

// Create a cURL handle
$ch = curl_init('http://example.com/csv1');

// Create a CURLFile object
$cfile = curl_file_create('localfilename.csv','text/csv','sentfilename.csv');

// Assign POST data
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Execute the handle
curl_exec($ch);

Just be aware: Sending cURL requests in PHP is blocking and not asynchronous as you stated in your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM