- Home /
Adding headers to Get request///
How can I add header information to make the GET request from Unity www class. I use headers in POST method as below and it works.. WWWForm form = new WWWForm(); form.AddField("email", email); form.AddField("password", password); byte[] rawData = form.data;
Hashtable headers = form.headers; headers["Game-Key"] = "549efcadf98"; headers["Game-Secret"] = "b6dc5%aa7b%a1ff#3696$";
WWW www = new WWW(url, rawData, headers);
Now, I am trying to get the gamer information with GET request and I've no idea of how to make it... I tried the same as before and WWWForm is for POST only.
Answer by delsolu · Sep 19, 2013 at 06:21 AM
The key is leaving "postData" parameter to null. From Unity3D WWW reference
The WWW class will use GET by default and POST if you supply a postData parameter.
So this one works for me:
var form = new WWWForm();
var headers = new Hashtable();
headers.Add("Header_key", "Header_val");
www = new WWW("http://localhost/getpostheaders", null, headers);
yield return www;
Debug.Log("2. " + www.text);
Answer by Numid · Aug 10, 2012 at 11:53 AM
Here's a hacky suggestion.
C# code executed from the editor
string url = "http://mydomain.com/myphpscript.php?param1=value1¶m2=value2";
Hashtable headers = new Hashtable();
headers.Add("Cookie", "PHPSESSID=qo62537o8djtojba7f05pu8u83");
WWW www = new WWW(url, new byte[] {(byte) 0}, headers);
yield return www;
Debug.Log(www.text);
PHP back-end script
<?php
print_r(getallheaders());
print_r($_GET);
?>
Output in console
Array
(
[User-Agent] => UnityPlayer/3.5.2f2 (http://unity3d.com)
[Host] => mydomain.com
[Accept] => */*
[Content-Length] => 1
[Cookie] => PHPSESSID=qo62537o8djtojba7f05pu8u83
[Content-Type] => application/x-www-form-urlencoded
)
Array
(
[param1] => value1
[param2] => value2
)
The request method is POST, but PHP seems to retrieve the GET parameters anyway.
This may depend on your server-side setting (language and configuration). However this is worth trying.
In WWW constructor, we can't able to pass null otherwise it's firing 404 Not Found error always. By passing 0 byte data, problem get resolved. Really thanks for this reply.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133155im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
HTTP Response Headers? 3 Answers
NullReferenceException error, GameObject.Find. 1 Answer
how to get the child of an instanteated object 2 Answers
Object Reference Required To Access A Non-Static Field 1 Answer
Get prefab through script 1 Answer