Please Help: Error:The remote server returned an error: (415) Unsupported Media Type.

Hello,

I am trying to run C# to submit information into our API. However, we keep getting the following error: Message Returned:
Error:The remote server returned an error: (415) Unsupported Media Type.

Obviously we are missing something in the code. Any ideas? Of course, any confidential information is redacted :slightly_smiling_face:

private string APIKey = “<HIDDEN>”;
private string sNotesUrl = “https://api.hubapi.com/crm/v3/objects/contacts/”;

private bool CreateCallNote(DateTime dActivityDate, string sDesc, string sSubject, string sPSID, string sDirection, string sDisp)
{
bool result = true;
// build json
ServiceLogger.Log(sLogFile, “Creating Temp Classes”);
ServiceLogger.Log(sLogFile, “Creating Note Object:”);
LeadNote NoteMain = new LeadNote();
ServiceLogger.Log(sLogFile, “Setting ID:” + skStudent);
NoteMain.Vendorid = Int32.Parse(skStudent);

ServiceLogger.Log(sLogFile, “Setting Description:”);
NoteMain.description = sDesc;
ServiceLogger.Log(sLogFile, “Survcode:”);
NoteMain.survcode = sSurvcode;
ServiceLogger.Log(sLogFile, “Disp Time:”);
NoteMain.lastdispositioned = dActivityDate.ToShortDateString();

sURLOut = sNotesUrl;

ServiceLogger.Log(sLogFile, “Sending Up Json I hope:” + NoteMain.ToString());

string json = JsonConvert.SerializeObject(NoteMain, Formatting.None);
json=“{\“properties\”:” +json + “}”;

ServiceLogger.Log(sLogFile, “Bare String:” + json);
// Transmist and get results
WebRequest objRequest;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
objRequest = WebRequest.Create(sURLOut + sClientid + @“?hapikey=” + APIKey);

objRequest.Method = “PATCH”;
objRequest.ContentLength = json.Length;
objRequest.ContentType = “application/json’”;
StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream(), Encoding.ASCII);
myWriter.Write(json);

myWriter.Close();
WebResponse objResponse = objRequest.GetResponse();
StreamReader sr;
Encoding encode = System.Text.Encoding.GetEncoding(“utf-8”);
sr = new StreamReader(objResponse.GetResponseStream(), Encoding.ASCII);
string strresult = “”;
strresult = sr.ReadToEnd();
ServiceLogger.Log(sLogFile, “Posted Response:” + strresult);
strresult = strresult.ToUpper();
strresult = strresult.Trim();
return (result);
}

public class LeadNote
{
public int Vendorid { get; set; }
public string survcode { get; set; }
public string description { get; set; }
public string lastdispositioned { get; set; }
}

json passed
{“properties”:{“Vendorid”:999999,“survcode”:“TEST”,“description”:“Testing Call Result”,“lastdispositioned”:“4/19/2021”}}

Message Returned:
Error:The remote server returned an error: (415) Unsupported Media Type.

your content-type is application/json but you are sending string data

string json = JsonConvert.SerializeObject(NoteMain, Formatting.None);

StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream(), Encoding.ASCII);
myWriter.Write(json);

in your code json variable is a string type, but API endpoint accept application/json format

Thank you! Turns out we also had to change to byte? Strange but thanks for your help!

Can you please say how you solved this problem? What modifications did you make to the code?