WWWForm: can't get it to work with Slack Webhooks

Dear Unity peers,

Inside one of my game scripts, I am having difficulty sending info to a slack feed with an HTTP POST request and JSON. I couldn’t locate any discussion of this specific issue in the forums and am hopeful someone might be able to help. I am using the following C# code:

	WWWForm form = new WWWForm ();
	form.AddField ("text", "hello world");
	WWW link = new WWW ("https://hooks.slack.com/services/00000000000000000/XXXXXXXXXXXXXXXXXXXX", form);
	yield return link;
	if (!string.IsNullOrEmpty (link.error)) {
		Debug.Log ("Error sending POST request: " + link.error + " " + link.text);
	}
	Debug.Log ("post attempt is done");

I can get something similar to work with curl, and this runs properly in my script, but I keep getting HTTP 500 errors with the text “invalid payload” when I attempt this in my unity C# script as above. I did notice that with curl I have to use “url-encode”… but my understanding of WWWForm takes care of this automatically. For reference, here is Slack’s example of proper webhook usage:

curl -X POST --data-urlencode 'payload={"text":"This is a line of text.

And this is another one."}’ https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX

There’s more discussion of Slack webhooks here, though it didn’t seem to help: Sending messages using Incoming Webhooks | Slack
Does anyone see what I might be doing wrong, or have any suggestions for further debugging the problem?

I am experiencing the same issue - did you make any progress on this? :slight_smile:

EDIT:
I got it working.

private static IEnumerator PostToSlackWebHook(string message)
{
	WWWForm slackPost = new WWWForm();
	slackPost.headers["Content-Type"] = "application/x-www-form-urlencoded";
	slackPost.AddField("payload", JsonUtility.ToJson(new Payload
	{
		text = message
	}));
	UnityWebRequest www = UnityWebRequest.Post(_slackWebhookURL, slackPost);

	var operation = www.SendWebRequest(); 
	
	yield return operation.isDone;
	
	if (www.isNetworkError || www.isHttpError)
	{
		Debug.Log($"Slack Webhook | {www.error} ({www.downloadHandler.text})");
	}
}