#Unity Web Request

1 messages · Page 1 of 1 (latest)

snow quest
#

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...

ripe thicket
#

to start with your ProjectItemRequest is not Serializable

#

Debugging your jsonBody instead of just assuming it was correct should have been your first action

snow quest
# ripe thicket to start with your ProjectItemRequest is not Serializable

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

ripe thicket
#

should you not be doing a POST rather than a GET ?

snow quest
#

if im using POST i'll get MethodNotAllowed error.

ripe thicket
#

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

snow quest
ripe thicket
#

well that looks like your backend is not returning anything so I would look there, especially in the web server log files

snow quest
#

i really doubt that because in postman it does return a list of projects.

#

so im still guessing that i may have missed something.

ripe thicket
snow quest
#

Thank you i;ll try it now...

snow quest
#

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
}
ripe thicket
#

That is what I said, you cannot send a content with the GET verb

snow quest
#

But if i didnt set the content i would get error 415 huhuhu this is so confusing

robust ember
#

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.

snow quest
#

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