将curl命令转换为restsharp

时间:2017-01-08 11:24:37

标签: c# php rest curl restsharp

如何将此curl命令转换为restsharp?

curl -X POST \
--header "Authorization: Bearer SomeToken" \
--header "_Id: SomeId" \    
--data-urlencode 'keys: name, age' \
https://api.SomeDomain.com/object-storage/classes/query/Player

我试过这个:

RestClient client = new RestClient("https://api.SomeDomain.com");
RestRequest request = new RestRequest("/object-storage/classes/query/hotels", Method.POST);
request.AddHeader("_Id", "SomeId");
request.AddHeader("Authorization", "Bearer " + SomeToken);
request.AddParameter("application/json", "{\"keys\" : \"name\" ,\"age\"}");

var response = client.Execute(request);

我不知道如何转换这一行:

--data-urlencode 'keys: name, age' \

编辑1:

好的,因为@Evk说我尝试了curl commond:

curl -X POST \
--header "Authorization: Bearer SomeToken" \
 --header "X-Backtory-Object-Storage-Id: SomeId"  \
 --data-urlencode 'keys:Name' \
 --trace-ascii /dev/stdout 
 https://api.backtory.com/object-storage/classes/query/hotels

这是跟踪打印:

 => Send header, 908 bytes (0x38c)
 0000: POST /object-storage/classes/query/hotels HTTP/1.1
 0034: Host: api.backtory.com
 004c: User-Agent: curl/7.52.1
 0065: Accept: */*
 0072: Authorization: Bearer SomeToken
 030d: X-Backtory-Object-Storage-Id: SomeId
 0345: Content-Length: 17
 0359: Content-Type: application/x-www-form-urlencoded
 038a:
 => Send data, 17 bytes (0x11)
 0000: %27keys%3AName%27

和错误:

 {
 "timestamp" : "2017-01-13T13:59:29.883UTC",
 "status" : 500,
 "error" : "Internal Server Error",
 "exception" : "com.google.gson.JsonSyntaxException",
 "message" : "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 19 path $",
 "path" : "/classes/query/hotels"
 }

并且对于更多细节我在服务器端无法控制!

2 个答案:

答案 0 :(得分:1)

data-urlencode的{​​p> curl只会对您传递给它的内容进行网址编码并将其写入正文。您可以查看:

curl -X POST --data-urlencode 'keys:name, age' --trace-ascii /dev/stdout  https://api.SomeDomain.com/object-storage/classes/query/Player

请求将是:

0000: POST / HTTP/1.1
0011: User-Agent: curl/7.29.0
002a: Host: google.com
003c: Accept: */*
0049: Authorization: Bearer SomeToken
006a: _Id: SomeId
0049: Content-Length: 20
005d: Content-Type: application/x-www-form-urlencoded
0000: keys%3Aname%2C%20age < here is your body

要对RestSharp执行相同操作,请对自己进行编码并传递为body参数:

RestClient client = new RestClient("http://api.SomeDomain.com");
RestRequest request = new RestRequest("/object-storage/classes/query/hotels", Method.POST);
request.AddHeader("_Id", "SomeId");
request.AddHeader("Authorization", "Bearer " + "SomeToken");
request.AddParameter("application/x-www-form-urlencoded", HttpUtility.UrlEncode("keys: name, age"), ParameterType.RequestBody);

var response = client.Execute(request);

请求将是:

_Id: SomeId
Authorization: Bearer SomeToken
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp/105.2.3.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 19
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

keys%3a+name%2c+age < your body. Spaces are encoded as "+" instead of "%20" but that should be ok

如果这不起作用,请准确发布产生的错误消息(如果有)。

答案 1 :(得分:1)

你试过这种方式吗?指定请求是否接受json?

RestClient client = new RestClient("https://api.SomeDomain.com");
RestRequest request = new RestRequest("/object-storage/classes/query/hotels", Method.POST);
**request.AddHeader("Accept", "application/json");**

request.AddHeader("_Id", "SomeId");
request.AddHeader("Authorization", "Bearer " + SomeToken);
request.AddParameter("application/json", "{\"keys\" : \"name\" ,\"age\"}");

var response = client.Execute(request);