1

I am facing issue while sending csv file to api. when i use simple php curl request it works fine for me and it sends the file to server. here is my php code

$curl = curl_init();
curl_setopt_array( $curl, array(
    CURLOPT_URL => $curlurl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => array(
        'csv_file' => new CURLFILE($csv_file_path), 
        'filename' => $file_name, 
        'website' => $site_url
    ),
    CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer ' . $doobert_api_token_value . ''
    ),
));

$response = curl_exec($curl);
curl_close($curl);

but when i try to send same file using wp_remote_post it calls api but i does not send file to request. here is my wordpress code

$request_body = array(
    'body' => array(
        'csv_file' => new \CURLFile($csv_file_path, 'text/csv', $file_name.'.csv'),
        'filename' => $file_name,
        'website' => $site_url,
    ),
);
                
print_r($request_body);
    
$request_args = array(
    'headers' => array(
        'Authorization' => 'Bearer ' . $doobert_api_token_value,
        'Content-Disposition' => 'form-data; name="file"; filename="' . $file_name . '"',
    ),
    'body' => $request_body['body'],
);
    
$response = wp_remote_post($curlurl, $request_args);
         
$response_body = wp_remote_retrieve_body($response);

anyone can solve this issue?

1
  • If the first code is working - what is the desire to change it to the second snippet?
    – Adam
    Mar 20 at 21:07

1 Answer 1

0

mirror issue in request body and headers ou can adjust your code to properly send the CSV file using wp_remote_post()

The csv_file parameter is set to a CURLFile object, which represents the file to be uploaded. This should correctly include the CSV file in the request body.

// Set up the request body
$request_body = array(
    'csv_file' => new CURLFile($csv_file_path, 'text/csv', $file_name.'.csv'),
    'filename' => $file_name,
    'website' => $site_url,
);

// Set up the request headers
$request_headers = array(
    'Authorization' => 'Bearer ' . $doobert_api_token_value,
);

// Set up the request args
$request_args = array(
    'headers' => $request_headers,
    'body' => $request_body,
);

// Send the request
$response = wp_remote_post($curlurl, $request_args);

// Check for errors
if (is_wp_error($response)) {
    $error_message = $response->get_error_message();
    echo "Error: $error_message";
} else {
    // Handle response
    $response_code = wp_remote_retrieve_response_code($response);
    $response_body = wp_remote_retrieve_body($response);
    echo "Response Code: $response_code\n";
    echo "Response Body: $response_body\n";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.