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:
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.
CodePudding user response:
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;