2

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 !

behrooz
  • 617
  • 9
  • 30
  • `keys: name, age` looks like simple text which url-encoded by curl. So you have to encode this text and pass it as query body. And, probably, to set "Content-Type: application/x-www-form-urlencoded" header too. – George Sovetov Jan 08 '17 at 12:00
  • @GeorgeSovetov thank you but i don't understand what you mean. request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddParameter ("application/json", "{\"keys\" : \"name\" , \"age\"}"); – behrooz Jan 08 '17 at 12:34
  • Here is how to post data: http://stackoverflow.com/a/21406569/1833960. But you need another content type. – George Sovetov Jan 08 '17 at 12:52
  • Not sure but may be try this request.addparameter("application/x-www-form-urlencoded", HttpUtility.HtmlEncode("key:name,age"), ParameterType.RequestBody) instead of request.AddParameter ("application/json","{\"keys\" : \"name\" ,\"age\"}"); – Allen King Jan 12 '17 at 05:46
  • @AllenKing I tried Html Encode ... didn't work – behrooz Jan 12 '17 at 06:32
  • Describe what you are trying to do. Now you post some curl command which does not work and want to convert it to RestSharp. Why? It seems from error message that server expects json, but when you post json (as per comments) - it also does not work. What error is produced? – Evk Jan 14 '17 at 07:22
  • Just as an advice you can try using POSTMAN client is a chrome app that let you build rest command and can convert them in curl and restsharp command – hpfs Jan 14 '17 at 08:14

2 Answers2

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);
hpfs
  • 486
  • 9
  • 14
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).

Evk
  • 98,527
  • 8
  • 141
  • 191
  • thanks. But i got this error { "timestamp" : "2017-01-13T05:26:09.817UTC", "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 14 path $", "path" : "/classes/query/hotels" – behrooz Jan 13 '17 at 05:27
  • But it works with that exact curl command? Because it seems it expects json, but curl command does not pass json either. – Evk Jan 13 '17 at 05:30
  • yeah you are right ... the curl command didn't work either. I Edited my question check it. – behrooz Jan 13 '17 at 14:14