#Unity Web Request
1 messages · Page 1 of 1 (latest)
so now i tried to make a simple one like this
https://pastecode.io/s/n9ahpi9r
and here's how i tried to call it
StartCoroutine(GetAsyncTest("https://www.myapi.com", new ProjectItemRequest()));
public class ProjectItemRequest
{
public string Keyword { get; set; }
public string CategoryType { get; set; } = "0";
public string AreaRef { get; set; } = "0";
}
i tried my API on postman it works as expected. but when it ried to do it using UnityWebRequest it wont do anything the resultl from this debug
Debug.Log($"Simple Response Code: {request.responseCode}");
Debug.Log($"Simple Response Text: {request.downloadHandler.text}");
return empty.
now im having out of an idea whats wrong with it...
to start with your ProjectItemRequest is not Serializable
Debugging your jsonBody instead of just assuming it was correct should have been your first action
hy sry for late reply, already make it serizalizbe by adding a Attribute [System.Serializable]
and yet it still didnt work at all.
i did add a new test api and it works as expected. now im still wondering why this api is not working at all even though i test it using postman and its show the result
updated code
responsecode 200
https://hatebin.com/hsthbbbgcm
should you not be doing a POST rather than a GET ?
no, i tried on postman using a GET request
if im using POST i'll get MethodNotAllowed error.
well this makes no sense
var jsonBody = Newtonsoft.Json.JsonConvert.SerializeObject(requestBody);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.GetAsync(url);
your content is a local variable and you cannot pass content using a GET request
wait i think you look in wrong code. that's an old code, i update my new one on here
https://hatebin.com/hsthbbbgcm
im sorry for the confusing.
well that looks like your backend is not returning anything so I would look there, especially in the web server log files
i really doubt that because in postman it does return a list of projects.
so im still guessing that i may have missed something.
you may want to look at this
https://hatebin.com/dwsbyjztwh
it uses HttpClient
Thank you i;ll try it now...
i try to make a simple one got an error saying Cannot send a content-body with this verb-type.
private async void MakeRequest()
{
try
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "Token");
HttpRequestMessage requestMessage = new HttpRequestMessage
{
RequestUri = new Uri("myAPI"),
Method = HttpMethod.Get,
Headers = { { HttpRequestHeader.Accept.ToString(), "application/json" } },
Content = new StringContent(JsonConvert.SerializeObject(new ProjectItemRequest()), System.Text.Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(requestMessage);
Debug.Log($"Response : {response}");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
var test = JsonConvert.DeserializeObject<ProjectItem>(responseBody);
Debug.Log(responseBody);
Debug.Log(test);
}
else
{
Debug.Log("Failed to get response");
}
response.Dispose();
requestMessage.Dispose();
}
catch (Exception e)
{
Debug.Log(e.Message);
Debug.Log($"Error : {e}");
}
}
so i tried debugging
Cannot send a content-body with this verb-type.
Error : System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
at System.Net.HttpWebRequest.MyGetRequestStreamAsync (System.Threading.CancellationToken cancellationToken) [0x00070] in <f7bfd758b02a4936b0078adb4cb60396>:0
at System.Net.HttpWebRequest.RunWithTimeout[T] (System.Func`2[T,TResult] func) [0x0000d] in <f7bfd758b02a4936b0078adb4cb60396>:0
at System.Net.HttpWebRequest.GetRequestStreamAsync () [0x00000] in <f7bfd758b02a4936b0078adb4cb60396>:0
at System.Net.Http.MonoWebRequestHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00312] in <3f34347f51c3476589cbcd8e67ddd03e>:0
at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x000e8] in <3f34347f51c3476589cbcd8e67ddd03e>:0
at MainMenu.MakeRequest () [0x000ae] in F:\Git\ProjectB\Assets\Script\Menu\MainMenu.cs:42
UnityEngine.Debug:Log (object)
MainMenu/<MakeRequest>d__6:Mo
}
That is what I said, you cannot send a content with the GET verb
But if i didnt set the content i would get error 415 huhuhu this is so confusing
HTTP GET specification says no content body - pretty straight forward.
However, some services may not stick to the rules and allow you to break them
Bottom line: if you don't want problems, stick to the rules.
huhu since i cant get around with this i m gonna chat with the backend dev to change it to POST after reading this post on stackoverflow.
https://stackoverflow.com/questions/55473838/httpclient-statuscode-415-reasonphrase-unsupported-media-type