简体   繁体   English

RestSharp - 错误地将字符串转换为 json c#

[英]RestSharp - bad converting string to json c#

Hello dunno why but when i try POST with RestSharp there is something bad with converting data.您好,不知道为什么,但是当我尝试使用 RestSharp 进行 POST 时,转换数据时出现了问题。

I'm trying to post data with baselinker:我正在尝试使用 baselinker 发布数据:

API : https://api.baselinker.com/index.php?tester When i just post with thier test on website it's good response : API: https ://api.baselinker.com/index.php?tester 当我刚刚在网站上发布他们的测试时,反应很好: 在此处输入图像描述

But when i try do the same via RestSharp with code :但是当我尝试通过 RestSharp 使用代码做同样的事情时:

public async Task xdAsync()
{
    var client = new RestClient("https://api.baselinker.com/connector.php");
    string token = "my API token here";


    RestRequest request = new RestRequest();




    request.AddParameter("token", token);
    request.AddParameter("method", "addProductVariant");
    request.AddParameter("application/json","{\"storage_id\":\"bl_1\",\"product_id\":\"67564668\",\"variant_id\":1055421650,\"name\":\"42\",\"quantity\":5,\"price_brutto\":599.99,\"ean\":29193201490}",ParameterType.RequestBody);


 
    var response = await client.PostAsync(request);

    Console.WriteLine(response.Content);
}

It's give me just reponse like this :它给我的只是这样的回应:

{"status":"ERROR","error_code":"ERROR_STORAGE_ID","error_message":"Invalid storage identifier provided."}

I think there is an error with converting string or something.我认为转换字符串或其他东西有错误。 I tried a lot of changes without slahes , trying use @ and still can't find solution maybe someone had similar problem with RestSharp or smoething?我尝试了很多没有 slahes 的更改,尝试使用@并且仍然找不到解决方案,也许有人对 RestSharp 或 smoething 有类似的问题?

Also when i try another method via另外,当我尝试另一种方法时

  //wyslanie requesta:
            var url = "https://reqbin.com/echo/post/json";

            var httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Method = "POST";

            httpRequest.Accept = "application/x-www-form-urlencoded";
            httpRequest.Headers["Authorization"] = "My API KEY";
            httpRequest.ContentType = "application/json";
            var data = @"{
                            ""addProductVariant"":{ ""storage_id"":""bl_1"",
                                                    ""product_id"":""67564668"",
                                                    ""variant_id"":""10525421650"",
                                                    ""name"":""42"",
                                                    ""quantity"":""10"",
                                                    ""price_brutto"":""599.99"",
                                                    ""ean"":""29193201490"" }
                         } ";

            using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
            {
                streamWriter.Write(data);
            }


            var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Console.WriteLine(result.ToString());
            }

            Console.WriteLine(httpResponse.StatusCode);

I get reposne via httpRequest like this :我通过httpRequest得到 reposne,如下所示:

{"success":"true"}
OK

I don't have full response in this method but it works.我对这种方法没有完全回应,但它有效。

you have to add content type你必须添加内容类型

var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer "+token);

.... your code

var body="{\"storage_id\":\"bl_1\",\"product_id\":\"67564668\",\"variant_id\":1055421650,\"name\":\"42\",\"quantity\":5,\"price_brutto\":599.99,\"ean\":29193201490}";

request.AddParameter("application/json",body,ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var json=response.Content;

If you are using the latest RestSharp, you should not add the body parameter like you do, neither should you add the content type header.如果您使用的是最新的 RestSharp,则不应像添加 body 参数那样添加内容类型标头。

var client = new RestClient("https://api.baselinker.com/connector.php");
var token = "my API token here";
var request = new RestRequest()
    .AddParameter("token", token);
    .AddParameter("method", "addProductVariant");
    .AddStringBody("\"storage_id\":\"bl_1\",\"product_id\":\"67564668\",\"variant_id\":1055421650,\"name\":\"42\",\"quantity\":5,\"price_brutto\":599.99,\"ean\":29193201490}", DataFormat.Json);

var response = await client.PostAsync(request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM