0

Hello dunno why but when i try POST with RestSharp there is something bad with converting data.

I'm trying to post data with baselinker:

API : https://api.baselinker.com/index.php?tester When i just post with thier test on website it's good response : enter image description here

But when i try do the same via RestSharp with code :

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?

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 :

{"success":"true"}
OK

I don't have full response in this method but it works.

3
  • A good debugging tip. Get it working in POSTMAN (or Restsharp or whatever UI tool). Do a "export to code" or "show me the code". And look for all magic-values. GET vs POST and all those pesky HEADERS (names and values). Once you have the UI work..you "mimic" in the code. But you mimic everything..not bits and pieces. Commented Jun 20, 2022 at 20:26
  • Once you get past this issue, note that you have ""quantity"":""10"", ""price_brutto"":""599.99"" and ""ean"":""29193201490"". They are all numbers, not strings in the sample parameters you show Commented Jun 20, 2022 at 21:12
  • @granadaCoder thank you for suggestion. Postman is the best to do that . And give ready code in many languages to do that :) thanks so much Commented Jun 21, 2022 at 12:09

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

So yeah, see my comment (on the question).. you'll probably see this Header in our UI tool, and you need to code it like this. Upvote for this answer.
I just added content type and now i have error : {"status":"ERROR","error_code":"ERROR_EMPTY_TOKEN","error_message":"No user token provided."} . Seems like not see token now.
@AdamZbudniewek I don't know what kind of token are you using, but I showed how to add JWT token
Contet-Type should be application/x-www-form-urlencoded
0

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.