C# RestSharp 上传图片到 Laravel API
C# RestSharp uploading image to Laravel API
我有一个 Laravel API (PHP),我想从我的 Xamarin.Forms android 应用程序上传一张图片。我有以下代码:
public void UploadImage(string filePath) {
var httpClient = Globals.g_HttpClient;
var request = new RestRequest("upload_attachments");
request.Method = Method.PUT;
request.AddHeader("KEY", Globals.APIKey);
request.AddFile("File", filePath);
request.AddHeader("Content-Type", "multipart/form-data");
var response = httpClient.Put(request);
var contentResponse = Convert.ToString(response.Content);
Console.WriteLine(contentResponse);
}
我了解到request.AddFile()
的参数分别是name
和path-to-actual-file
,但是好像没有发送附件。我的请求附加了 0 个文件,我使用 Laravel 中的 dd($request)
查看实际请求。这是 dd
的 return:
Illuminate\Http\Request {#50
...
+request: Symfony\Component\HttpFoundation\ParameterBag {#51
#parameters: []
}
...
+files: Symfony\Component\HttpFoundation\FileBag {#55
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\ParameterBag {#53
#parameters: []
}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#56
#headers: array:7 [
"key" => array:1 [
0 => "KcUup1OR8zRwI99BNRtapQJF8xPMsHj1"
]
"accept" => array:1 [
0 => "application/json, text/json, text/x-json, text/javascript, application/xml, text/xml"
]
"user-agent" => array:1 [
0 => "RestSharp/106.11.4.0"
]
"content-type" => array:1 [
0 => "multipart/form-data; boundary=-----------------------------28947758029299"
]
"content-length" => array:1 [
0 => "20426"
]
"host" => array:1 [
0 => "loan.phcci.coop"
]
"accept-encoding" => array:1 [
0 => "gzip, deflate"
]
]
#cacheControl: []
}
...
}
如您所见,+files
return 是一个空数组。如果请求中包含任何文件,这就是文件应该位于的位置。
这个问题的答案实际上是因为我在应该使用 POST
的时候使用了 PUT
方法。我通常使用 PUT
创建资源,但是当有附加文件时它不起作用。
我有一个 Laravel API (PHP),我想从我的 Xamarin.Forms android 应用程序上传一张图片。我有以下代码:
public void UploadImage(string filePath) {
var httpClient = Globals.g_HttpClient;
var request = new RestRequest("upload_attachments");
request.Method = Method.PUT;
request.AddHeader("KEY", Globals.APIKey);
request.AddFile("File", filePath);
request.AddHeader("Content-Type", "multipart/form-data");
var response = httpClient.Put(request);
var contentResponse = Convert.ToString(response.Content);
Console.WriteLine(contentResponse);
}
我了解到request.AddFile()
的参数分别是name
和path-to-actual-file
,但是好像没有发送附件。我的请求附加了 0 个文件,我使用 Laravel 中的 dd($request)
查看实际请求。这是 dd
的 return:
Illuminate\Http\Request {#50
...
+request: Symfony\Component\HttpFoundation\ParameterBag {#51
#parameters: []
}
...
+files: Symfony\Component\HttpFoundation\FileBag {#55
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\ParameterBag {#53
#parameters: []
}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#56
#headers: array:7 [
"key" => array:1 [
0 => "KcUup1OR8zRwI99BNRtapQJF8xPMsHj1"
]
"accept" => array:1 [
0 => "application/json, text/json, text/x-json, text/javascript, application/xml, text/xml"
]
"user-agent" => array:1 [
0 => "RestSharp/106.11.4.0"
]
"content-type" => array:1 [
0 => "multipart/form-data; boundary=-----------------------------28947758029299"
]
"content-length" => array:1 [
0 => "20426"
]
"host" => array:1 [
0 => "loan.phcci.coop"
]
"accept-encoding" => array:1 [
0 => "gzip, deflate"
]
]
#cacheControl: []
}
...
}
如您所见,+files
return 是一个空数组。如果请求中包含任何文件,这就是文件应该位于的位置。
这个问题的答案实际上是因为我在应该使用 POST
的时候使用了 PUT
方法。我通常使用 PUT
创建资源,但是当有附加文件时它不起作用。