Php 将文件发布到 ServiceStack API
Php posting file to ServiceStack API
我需要帮助使用 PHP 将文件(doc、Docx 或 pdf)发布到 ServiceStack API。
php cURL 设置:
$curl = curl_init();
$cfile = new CURLFile('C:\test.doc');
$params = array('Data' => $cfile);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS,$params);
API 设置:
public object Any([FromBody]ExampleRequest exampleRequest){
...
}
public class ExampleRequest : IReturn<ExampleResponse>
{
public object Data { get; set; }
}
在 ExampleRequest
中使用 属性 对象类型的数据调用
我不确定为什么数据总是为空。任何帮助将不胜感激
[FromBody]
不是 ServiceStack 属性,对 ServiceStack 服务没有影响。
您的类型化请求 DTO 需要简单地匹配 ExampleRequest
的架构。所有 ServiceStack 服务都可以由 QueryString, FormData, or via Request Body in any of the default registered Content-Types 如 JSON 填充。您需要确保您的请求发送与其 Content-Type
HTTP 请求 Header.
匹配的正确格式
建议您永远不要使用 object
,否则您需要考虑 custom serialization behavior and security restrictions 以反序列化 object
类型。
通过定义 Data
属性,您告诉 ServiceStack 接受带有 Data 属性 的请求(例如 JSON):
{"Data": ...}
If sending flat Key/Value pairs, consider using Dictionary<string,string>
instead of object
自己解析原始请求Body
你的请求 DTO 应该指定模式的类型,如果你不想这样做,你可以告诉 ServiceStack 跳过反序列化请求 Body 并让你的服务解析原始请求 Body 通过 Request DTO implement IRequiresRequestStream,例如:
//Request DTO
public class ExampleRequest : IRequiresRequestStream
{
Stream RequestStream { get; set; }
}
实施:
public object Any(ExampleRequest request)
{
byte[] bytes = request.RequestStream.ReadFully();
string text = bytes.FromUtf8Bytes(); //if text was sent
}
我需要帮助使用 PHP 将文件(doc、Docx 或 pdf)发布到 ServiceStack API。
php cURL 设置:
$curl = curl_init();
$cfile = new CURLFile('C:\test.doc');
$params = array('Data' => $cfile);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS,$params);
API 设置:
public object Any([FromBody]ExampleRequest exampleRequest){
...
}
public class ExampleRequest : IReturn<ExampleResponse>
{
public object Data { get; set; }
}
在 ExampleRequest
中使用 属性 对象类型的数据调用我不确定为什么数据总是为空。任何帮助将不胜感激
[FromBody]
不是 ServiceStack 属性,对 ServiceStack 服务没有影响。
您的类型化请求 DTO 需要简单地匹配 ExampleRequest
的架构。所有 ServiceStack 服务都可以由 QueryString, FormData, or via Request Body in any of the default registered Content-Types 如 JSON 填充。您需要确保您的请求发送与其 Content-Type
HTTP 请求 Header.
建议您永远不要使用 object
,否则您需要考虑 custom serialization behavior and security restrictions 以反序列化 object
类型。
通过定义 Data
属性,您告诉 ServiceStack 接受带有 Data 属性 的请求(例如 JSON):
{"Data": ...}
If sending flat Key/Value pairs, consider using
Dictionary<string,string>
instead ofobject
自己解析原始请求Body
你的请求 DTO 应该指定模式的类型,如果你不想这样做,你可以告诉 ServiceStack 跳过反序列化请求 Body 并让你的服务解析原始请求 Body 通过 Request DTO implement IRequiresRequestStream,例如:
//Request DTO
public class ExampleRequest : IRequiresRequestStream
{
Stream RequestStream { get; set; }
}
实施:
public object Any(ExampleRequest request)
{
byte[] bytes = request.RequestStream.ReadFully();
string text = bytes.FromUtf8Bytes(); //if text was sent
}