Reputation: 15
Why is the response of REST API is not in JSON format in my code? But with Google extension "RestMan" it is in correct format. My code is like this:
using (WebClient client = new WebClient())
{
byte[] response = client.UploadValues("http://personnel.fasau.ac.ir:8085/PersonApi/api/Token/Login", new NameValueCollection()
{
{"Username", "name"},
{"Password", "pass"}
});
string result = System.Text.Encoding.UTF8.GetString(response);
}
The response I get is like this:
\u001f�\b\0\0\0\0\0\u0004\05��n�P\u0010�_�b�D\\�#�3�`��1\u001c\u000e\aÎ�[.\a�\b\u000e6Q\u0017ݴY�=��*+}\u0018��Lq��f4��#���0$�i�<6�a7f�>ߕ�#�;�I���M�kN�q0՞�\u0012��\\�jy�%s]�\u001fD\\X�c�Oa�6�=,\v�À(\u000e�ׁbՖ[�إ+ª�K�銕*�A\u001b�t�/��+Z�pPe\u0003\t�\u0005h �w\u0006�:TX��I�e��a��JR;\u000e�C%Yc\"/\u0093T�*�\u0002Jr��ҥ2@%��lv2�X�\u0001R1�8䴓��Ǎ�8\u0001�]�ru�hD��\u000fv�`ċ.��f�Gm�� �u=����\u001f�\u0004#N��\f�!�z\u001b\u0017�\u0001�uCa��\\\a�\u0016��B\u0015���it�\u001f��K7\u0019�\r%\u0017�\u0012�\u0006����Ln�\r}��\u0011\a��G��iબ��0����\u0004C\u0001�p��\u0004�\u0016�<Z�.�}v]���\u001e \u0014*������\u0012>��u�x��J��6$�������ML�m��T�7�S�3\u0019��>�\u001a`<��\u00193�R\u0018\u0014�������_���2���o����\\_����\a���=�X\u000e�1\u0003�z_㴡7[��?_���?n����u�/\u007f\u0001b�Q�S\u0002\0\0
The response body of "RestMan" is like this (in correct format):
{
"Valid": true,
"Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6Im56RDhteWJaUkVkV2Fsdmppa2hCM0Y5RHdnQWMyRm5UczZja3ZMUjNiUkxFaTUwUGNrYUt3VTdoZHpKbS9KM1FlYkR0ZDhkZUlSZjBFMHdHcUJJRE01VVRld1JDS3pZOWU0akxKMzhBSmY3MVBQaU9RdG50YXNzYlV0Q0pRK2dCUlJ1a01oQTBUKzhFWnkvenNkQ0NUbTYzR01GTFR2S2NwOGtLUVh0ZmJ1WT0iLCJEaXNwbGF5TmFtZSI6IlciLCJuYmYiOjE2MDk2ODI3NDksImV4cCI6MTYwOTY5MzU0OSwiaWF0IjoxNjA5NjgyNzQ5LCJpc3MiOiJCcHRzb2Z0LmNvbSIsImF1ZCI6IkJwdHNvZnQuY29tIn0.4J0h30Gd1RzWl3tBulGWEQwcQW_PnTV0VTDFAb8S-vg",
"FullName": "کاربر وب سرویس",
"Photo": null,
"ErrorTitle": "دانشگاه فسا"
}
Upvotes: 1
Views: 2104
Reputation: 4011
Try setting an accept header,
client.Headers.Add("Accept", "application/json");
Update: It is also possible that the response in compressed using Gzip compression. Use a helper function to decompress the string first and then use a JSON serializer to serialize it.
A helper function I use to decompress Gzip in my code,
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
public string DecompressString(string s, Encoding encoding)
{
// obtaining bytes from the string in the below step
// In my case, it was a base 64 string
// If not use encoding.GetBytes() function
var bytes = Convert.FromBase64String(s);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
gs.CopyTo(mso);
}
return encoding.GetString(mso.ToArray());
}
}
var originalRes = // call your function here
var decompressedRes = helper.DecompressGzipString(originalRes, Encoding.UTF8);
Upvotes: 1
Reputation: 24913
Try unicode encoding:
string result = System.Text.Encoding.Unicode.GetString(response);
Upvotes: 1