英文:
C# RestSharp JSON Syntax
问题
我似乎在我的JSON主体中遇到了语法问题。
该代码旨在使用API从LetterXpress检索余额,但我没有能够进行身份验证,而是收到了以下响应:
("{"message":"未经授权。}")
在Postman中使用该请求正常工作。
以下是我的C#代码:
var client = new RestClient("https://api.letterxpress.de/v2/balance");
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
string jsonString = "{" +
"\"auth\": {" +
"\"username\" : \"LXPApi1\"," +
"\"apikey\" : \"442807b35e250ec767221fe3ba\"," +
"\"mode\" : \"live\"" +
"}" +
"}";
request.AddJsonBody(jsonString);
IRestResponse response = await client.ExecuteAsync(request);
string meinString = response.Content;
textBox4.Text = meinString;
使用此代码,我迄今为止尝试过。我怀疑jsonString
中的语法不正确。但是,我找不到错误。
英文:
I seem to have a syntax problem with my JSON body.
The code is intended to retrieve the balance from LetterXpress using an API, but instead of being able to authenticate, I receive the response:
> ("{"message":"Unauthorized."}")
The request works fine using Postman.
Here is my C# code:
var client = new RestClient("https://api.letterxpress.de/v2/balance");
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
string jsonString = "{" +
"\"auth\": {" +
"\"username\" : \"LXPApi1\"," +
"\"apikey\" : \"442807b35e250ec767221fe3ba\"," +
"\"mode\" : \"live\"" +
"}" + "}";
request.AddJsonBody(jsonString);
IRestResponse response = await client.ExecuteAsync(request);
string meinString = response.Content;
textBox4.Text = meinString;
With this code, I have tried so far. My suspicion is that the syntax in the jsonString
is incorrect. However, I cannot find the error.
答案1
得分: 1
"My suspicion is that the syntax in the jsonString is incorrect."
Close, it's one of the problems with your code.
JSON Encoded String!
AddJsonBody
expects an object that will be serialized automatically - see the documentation. What you're doing is effectively JSON encoding a string so the request will never be able to deserialize it to your expectations.
What you should be doing instead is pass an object that has all the expected properties. You can use an anonymous object, or a concrete class, for the request body:
var body = new {
auth = new {
username = "_LXPApi1_",
apikey = "_442807b35e250ec767221fe3ba_",
mode = "_live_"
}
};
request.AddJsonBody(body);
GET Request With Body
You're sending a GET request and attempting to include a body with the request which is not valid for GET requests. The chances are that you should be sending a POST request instead.
Unfortunately the API Documentation (in German) shows a GET request which most, if not all, HTTP clients will not support and are likely to not include the body of the request even though you have set it.
Side Notes
- I would advise that you never create JSON manually and always leverage existing JSON serialization tools to convert objects to JSON.
- Never expose any credentials in a question.
英文:
"My suspicion is that the syntax in the jsonString is incorrect." Close, it's one of the problems with your code.
JSON Encoded String!
AddJsonBody
expects an object that will be serialized automatically - see the documentation. What you're doing is effectively JSON encoding a string so the request will never be able to deserialize it to your expectations.
What you should be doing instead is pass an object that has all the expected properties. You can use an anonymous object, or a concrete class, for the request body:
var body = new {
auth = new {
username = "LXPApi1",
apikey = "442807b35e250ec767221fe3ba",
mode = "live"
}
};
request.AddJsonBody(body);
GET Request With Body
You're sending a GET request and attempting to include a body with the request which is not valid for GET requests. The chances are that you should be sending a POST request instead.
Unfortunately the API Documentation (in German) shows a GET request which most, if not all, HTTP clients will not support and are likely to not include the body of the request even though you have set it.
Side Notes
-
I would advise that you never create JSON manually and always leverage existing JSON serialization tools to convert objects to JSON.
-
Never expose any credentials in a question.
答案2
得分: 0
你正在不正确地构建请求
以下是一个良好的调用,但未经授权。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var options = new RestClientOptions("https://api.letterxpress.de")
{
UseDefaultCredentials = true,
//ThrowOnAnyError = true,
//MaxTimeout = 1000,
};
using (RestClient client = new RestClient(options))
{
string jsonString = "{" +
"\"auth\": {" +
"\"username\" : \"LXPApi1\"," +
"\"apikey\" : \"442807b35e250ec767221fe3ba\"," +
"\"mode\" : \"live\"" +
"}" + "}";
JsonConvert.DeserializeObject(jsonString).Dump();
var request = new RestRequest("/v2/balance", Method.Get)
.AddHeader("Content-Type", "application/json")
.AddBody(jsonString);
var response = client.ExecuteAsync(request).Result;
Console.Write(response.Content);
}
仍然未经授权,正如你提到的 - 代码在 Postman 请求中运行,请添加 Postman 请求的截图。
英文:
you are building request incorrectly
here is a good call, but not authorized.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var options = new RestClientOptions("https://api.letterxpress.de")
{
UseDefaultCredentials = true,
//ThrowOnAnyError = true,
//MaxTimeout = 1000,
};
using (RestClient client = new RestClient(options))
{
string jsonString = "{" +
"\"auth\": {" +
"\"username\" : \"LXPApi1\"," +
"\"apikey\" : \"442807b35e250ec767221fe3ba\"," +
"\"mode\" : \"live\"" +
"}" + "}";
JsonConvert.DeserializeObject(jsonString).Dump();
var request = new RestRequest("/v2/balance", Method.Get)
.AddHeader("Content-Type", "application/json")
.AddBody(jsonString);
var response = client.ExecuteAsync(request).Result;
Console.Write(response.Content);
}
still not authorized, as you mentioned - code runs wwith postman request please add a screenshot of postman request
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论