简体   繁体   English

CURL Fileupload REST API

[英]CURL Fileupload REST API

I am Trying to Call File Upload REST API Using Curl but it returns blank screen i dont know whats going wrong. 我正在尝试使用Curl调用文件上传REST API,但它返回黑屏,我不知道出了什么问题。 File Upload API Returns File Name if Success. 如果成功,文件上传API将返回文件名。 same thing i am able to call fule upload rest api using postman tool. 同样,我可以使用邮递员工具调用fule upload rest api。

Error: 错误:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Sequence contains no matching element",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": " at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)\r\n at PharmaRackv2.WebUI.Areas.Admin.Controllers.UploadController.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"
}

PHP Code: PHP代码:

try {
            $file = "D:\xampp\htdocs\api.pharmarack.com\distributors\11380\08122017123602.zip";
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "http://test.com/api/upload",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => array(
                'attachment' => curl_file_create($file),
            ),
                CURLOPT_HTTPHEADER => array(
                    "cache-control: no-cache",
                    "content-type: multipart/form-data",
                    "distributorcode: 1HUBH",
                    "foldername: 1HUBH"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
                echo "cURL Error #:" . $err;
            } else {
                echo $response;
            }
        } catch (Exception $e) {
            echo 'Message: ' . $e->getMessage();
        }

POSTMAN CALL It is working fine. POSTMAN CALL工作正常。 File Upload API Returns File Name if Success. 如果成功,文件上传API将返回文件名。 在此处输入图片说明 在此处输入图片说明

The problem is that DistributorCode and FolderName are case sensitive, that is why you are getting error in your curl. 问题在于DistributorCodeFolderName区分大小写,这就是为什么在curl中出错的原因。 fix that Like: 修复像:

CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data",
    "DistributorCode: 1HUBH",
    "FolderName: 1HUBH"
),

Edit: The problem is that with multipart/form-data , you need to send the data via boundries, you can't send it like file=abc.php , like usual post param. 编辑:问题是,使用multipart/form-data ,您需要通过边界发送数据,不能像通常的post param一样发送file=abc.php

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://ragabh.com/api/upload",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------FormData\r\nContent-Disposition: form-data; name=\"attachment\"; filename=\"E:\\c5-500x500.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------FormData--",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----FormData",
    "DistributorCode: 1HUBH",
    "FolderName: 1HUBH",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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