I've spent weeks trying to make a POST request from C# to an API, with no success. The example provided by the official documentation is the following:
curl -v -u MYTOKEN:api_token \
-H "Content-Type: application/json" \
-d '{"object":{"param1":"a","param2":2,"param3":3,"param4":true,"param5":5}}' \
-X POST APIURL
This is the way I'm currently trying to perform the request
var webAddr = "APIURL";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential("MYTOKEN", "api_token");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"object\":{\"param1\":\"a\",\"param2\":2,\"param3\":3,\"param4\":true,\"param5\":5}}";
streamWriter.Write(json);
streamWriter.Flush();
}
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
I've tested the request using CURL in a Linux Terminal and it works fine, so that's not the problem.
The error I'm getting is 403 "Forbidden". The documentation states that the credentials are to be used with the token as username and "api_token" as password.
Thanks!