问题
How to convert this curl command to 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
I've tried this:
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);
I don't have any idea how to convert this line:
--data-urlencode 'keys: name, age' \
EDIT 1:
ok as @Evk said i try the 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
this is what trace print :
=> 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
and the error :
{
"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"
}
and for more detail i have no control at server side !
回答1:
data-urlencode
of curl
will just url-encode what you pass to it and write it to the body. You can check it with:
curl -X POST --data-urlencode 'keys:name, age' --trace-ascii /dev/stdout https://api.SomeDomain.com/object-storage/classes/query/Player
Request will be:
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
To do the same with RestSharp, url encode yourself and pass as body parameter:
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);
Request will be:
_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
If that doesn't work, post exactly what error message is produced (if any).
回答2:
Have you tried this way ? Specifying that the request accept 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);
来源:https://stackoverflow.com/questions/41532171/convert-curl-command-to-restsharp