0

I am getting error when trying to send Post message

  string productJson = await Task.Run(() => JsonConvert.SerializeObject(product, Formatting.Indented));
  HttpContent content = new StringContent(productJson, Encoding.UTF8, "application/json");
  Uri uri = new Uri(string.Format(@"{0}/api/product/addProduct?token={1}", Configuration.ServerURL, _token.TokenValue));

  var response = await _client.PostAsync(uri, content);

Model of product:

[XmlRoot(ElementName = "Product")]
public class Product
{
    [JsonProperty(PropertyName = "name")]
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }
    [JsonProperty(PropertyName = "code")]
    [XmlElement(ElementName = "Code")]
    public string Code { get; set; }
    [JsonProperty(PropertyName = "ean")]
    [XmlElement(ElementName = "Ean")]
    public string Ean { get; set; }
    [JsonProperty(PropertyName = "description")]
    [XmlElement(ElementName = "Description")]
    public string Description { get; set; }
    [JsonProperty(PropertyName = "value1")]
    [XmlElement(ElementName = "Value1")]
    public string Value1 { get; set; }
    [JsonProperty(PropertyName = "value2")]
    [XmlElement(ElementName = "Value2")]
    public string Value2 { get; set; }
    [JsonProperty(PropertyName = "productExternalNumber")]
    [XmlElement(ElementName = "ProductExternalNumber")]
    public int? ProductExternalNumber { get; set; }
    [JsonIgnore]
    [XmlElement(ElementName = "ProductExternalType")]
    public int? ProductExternalType { get; set; }
    [JsonProperty(PropertyName = "id")]
    [XmlElement(ElementName = "Id")]
    public int? DefaultStageId { get; set; }
    [JsonProperty(PropertyName = "unitName")]
    [XmlElement(ElementName = "UnitName")]
    public string UnitName { get; set; }
    [JsonProperty(PropertyName = "sourceIntegrationCode")]
    [XmlElement(ElementName = "SourceIntegrationCode")]
    public string SourceIntegrationCode { get; set; }
    [JsonProperty(PropertyName = "externalStorehouseId")]
    [XmlElement(ElementName = "ExternalStorehouseId")]
    public string ExternalStorehouseId { get; set; }
}

My JSON after serialization:

{ "name": "Bruynzeel Twin Felt Tip Flamastry 20szt", "code": "BS-2020K20B", "ean": "2010000000090", "description": "", "value1": "", "value2": "", "productExternalNumber": 9, "id": 0, "unitName": "szt.", "sourceIntegrationCode": "XL", "externalStorehouseId": "2" }

I tried using Postman to send Post message and it worked.

Full error

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: A. Path '', line 0, position 0. at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at Y.PluginPackingIntegration.Controllers.BaseIntegrationController.<>c__DisplayClass9_0.b__0() at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute()

user3210023
  • 164
  • 13
  • 2
    The stack trace shows that the error is occurring on a call to `DeserializeObject`, which I'm guessing you're calling after you get the response back. If that response is not valid JSON, it would explain why you can see it just fine in Postman but can't deserialize it to a C# object in your code. – Todd Menier Nov 26 '18 at 14:16
  • 1
    Please show your code up to the call to `JsonConvert.DeserializeObject()`, including how you get your JSON string. Possibly you have a [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) at the beginning of the `response` string. If so, to process it correctly use a `StreamReader` as shown in [Encoding.UTF8.GetString doesn't take into account the Preamble/BOM](https://stackoverflow.com/q/11701341/3744182) and [How do I ignore the UTF-8 Byte Order Marker in String comparisons?](https://stackoverflow.com/a/2915239/3744182). – dbc Nov 26 '18 at 23:48

0 Answers0