0

I'm trying to send a json content inside the body. But unfortunately, I don't get the data in the server. Same data is received using postman tool.

Here is the code I'm running

private string callAPI(string function, string content)
{
    using (var httpClient = new HttpClient())
    {
        string url = function;
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", txt_sessionKey.Text);
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        httpClient.BaseAddress = new Uri(baseUrlV2);
        var json = new StringContent(content, Encoding.UTF8, "application/json");

        HttpRequestMessage request = new HttpRequestMessage
            {                    
                Method = HttpMethod.Put,
                RequestUri = new Uri(baseUrlV2+function),
                Content = json
            };
        HttpContent contentRes = httpClient.SendAsync(request).Result.Content;

        return contentRes.ReadAsStringAsync().Result;
    }
}

What am I missing here?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rami Khawaly
  • 126
  • 3
  • 14
  • please add the content string as this is probably an error in your json syntax – Technivorous Oct 05 '18 at 17:50
  • Possible duplicate of [Serialize and Deserialize Json and Json Array in Unity](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity) – Technivorous Oct 05 '18 at 17:52
  • 1
    What is the HTTP status code and result you get? Also it may help to show the postman console trace of the request. As it is there is no way to do anything but guess. – Crowcoder Oct 05 '18 at 17:55
  • Here is the json i'm sending {"requestID":"12345678", "data":{"content" : "{"configurationData":{"ip":"192.168.20.229"},"type":"configuration"}","method":"Fast"}} I'm reaching the server (mine) and getting a response, but without the json – Rami Khawaly Oct 05 '18 at 19:49
  • Well, i fixed the json format and now i'm still getting partial data. in addition now i'm getting the requestid the json is { "requestID": "12345678", "data": "ew0KICAidHlwZSI6ICJjb25maWd1cmF0aW9uIiwNCiAgImNvbmZpZ3VyYXRpb25EYXRhIjoge30NCn0=", "method": "Fast" } – Rami Khawaly Oct 05 '18 at 20:09

0 Answers0