I have a xamarin forms app that makes API calls, and when the api calls involve very small json strings they work perfectly. An example payload that works is:
{"command":"","route":"check_password","password":"password","sqlctype":"","service":"",data:""}
An example payload that doesn't work is:
{"command":"","route":"run_script","password":"","sqlctype":"","service":"Image_Processor",data:"base64 string that is 2MB in size"}
However, with the second payload the streamWriter doesn't succeed in writing it and hangs indefinitely. My code is here:
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"command\":\"\"," +
"\"route\":\""+route+"\","+
"\"password\":\""+password+"\"," +
"\"sqlctype\":\"\"," +
"\"service\":\""+service+"\"," +
"\"data\":\""+data+"\"}";
streamWriter.Write(json);
}
But the interesting part is that the API operation completes successfully in both cases
The cloudwatch logs for a run with the first payload are as follows:
START RequestId: ee583521-f17d-4143-9c27-031ab781ddae Version: $LATEST
END RequestId: ee583521-f17d-4143-9c27-031ab781ddae
REPORT RequestId: ee583521-f17d-4143-9c27-031ab781ddae Duration: 7.01 ms Billed Duration: 8 ms Memory Size: 128 MB Max Memory Used: 93 MB XRAY TraceId: 1-611ac895-4ba33ffa6e181b6e66821bfc...
With a returned body of
"T"
The cloudwatch logs for a run with the second payload are as follows:
START RequestId: d78b16de-1a0b-4b35-877a-2fd6437dcd71 Version: $LATEST
END RequestId: d78b16de-1a0b-4b35-877a-2fd6437dcd71
REPORT RequestId: d78b16de-1a0b-4b35-877a-2fd6437dcd71 Duration: 4159.55 ms Billed Duration: 4160 ms Memory Size: 128 MB Max Memory Used: 93 MB XRAY TraceId: 1-611ac940-61cca9b762d1a2ff6...
With a returned body of
"[{\"typ\": \"barcode\", \"outp\": \"0882658000546\", \"ymax\": \"0.6736111111111112\", \"ymin\": \"0.550843253968254\", \"xmax\": \"0.6736111111111112\", \"xmin\": \"0.3488756613756614\"}, {\"typ\": \"barcode\", \"outp\": \"A-0020-Z\", \"ymax\": \"0.4595734126984127\", \"ymin\": \"0.40153769841269843\", \"xmax\": \"0.6008597883597884\", \"xmin\": \"0.3194444444444444\"}, {\"typ\": \"barcode\", \"outp\": \"A-0010-Z\", \"ymax\": \"0.34077380952380953\", \"ymin\": \"0.29092261904761907\", \"xmax\": \"0.5929232804232805\", \"xmin\": \"0.328042328042328\"}]"
Since the API is clearly running and running successfully in both cases, why might the app be hanging at
streamWriter.Write(json)
with only the second payload?
It may also be useful to note that I tried to do this async too and it hung on client.postasync regardless of the payload (neither payload didn't hang). Code:
async Task<string> asyncAPI(string route, string password = "", string data = "", string service = "")
{
HttpClient client = new HttpClient();
Uri uri = new Uri("https://uidvxrcio9.execute-api.us-east-2.amazonaws.com/default/Blueline_General_API");
string json = JsonConvert.SerializeObject(new APIrequest(default, route, password, default, service, "dawdasdqwd2"));
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("x-api-key", "key");
HttpResponseMessage response = null;
response = await client.PostAsync(uri, content);
return await response.Content.ReadAsStringAsync();
}
Why are you trying to serialize by your own code? Use the Newtonsoft.Json nuget package and use the methods in that. When you install that they you can easily serialize all the public properties of a class. The code to serialize one of your classes is like:
If you are trying to serialize raw data....you can make a byte[] property in your class like: