[英]C# RestSharp uploading image to Laravel API
I have a Laravel API (PHP) and I want to upload an image from my Xamarin.Forms android app. I have a Laravel API (PHP) and I want to upload an image from my Xamarin.Forms android app. I have this code below:我在下面有这段代码:
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);
}
I understand that the parameters for request.AddFile()
is the name
and path-to-actual-file
respectively, but it seems that the attachments are not being sent.我知道request.AddFile()
的参数分别是name
和path-to-actual-file
,但似乎没有发送附件。 There are 0 files attached to my request, I view the actual request by using dd($request)
in Laravel.我的请求附加了 0 个文件,我在 Laravel 中使用dd($request)
查看实际请求。 This is the return for the dd
:这是dd
的回报:
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: []
}
...
}
As you can see, +files
returns an empty array.如您所见, +files
返回一个空数组。 That is where the files should be if there are any included in the request.如果请求中包含任何文件,那么这就是文件所在的位置。
The answer to this is actually because I'm using the PUT
method when I should be using POST
.答案实际上是因为我在应该使用POST
时使用了PUT
方法。 I usually use PUT
to create resources but it doesn't work when there are files attached.我通常使用PUT
来创建资源,但是当有附加文件时它不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.