将curl命令转换为restsharp [英] Convert curl command to restsharp

查看:100
本文介绍了将curl命令转换为restsharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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尝试过:

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' \

编辑1:

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

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

这是跟踪打印的内容:

 => 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"
 }

更详细地讲,我无法控制服务器端!

and for more detail i have no control at server side !

推荐答案

您尝试过这种方法吗?指定请求接受json吗?

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);

这篇关于将curl命令转换为restsharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆