ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins. Read more for important details.
ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins.Read more for important details.
Lieutenant Commander
Lieutenant Commander
12341 views

Receiving content was incorrectly formatted, expected application/json; charset=utf-8. Am passing..

Jump to solution

TL;DR:  Could someone provide a sample JSON request to add a comment to an issue?

I'm trying to post a comment to an issue using the REST API against Fortify v18.10.0187.  I'm using .NET 4.6.2 (C#) and Newtonsoft.Json v9.0.0 to encode the JSON.

I'm the first to admit that the mistake I'm making is probably trivial.

 public static string PostMessage(string url, string content)
 {
      HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
      requestMessage.Headers.Add("Authorization", Configuration.FortifyAuthorization.ToHeader());
      requestMessage.Content = new StringContent(content, Encoding.UTF8, JsonMediaTypeFormatter.DefaultMediaType.MediaType);

      HttpResponseMessage hrm = HttpClient.SendAsync(requestMessage).Result;
      hrm.EnsureSuccessStatusCode();
      HttpContent hc = hrm.Content;

      return hc.ReadAsStringAsync().Result;
 }

url is /ssc/api/v1/issues/{0}/comments, base server address is our SSC server, {0} is replaced with the issue number we're posting a comment against.  We're hitting the right place.  Configuration.FortifyAuthorization.ToHeader() returns "FortifyToken" + the BASE64 encoded version of the token retrieved via /ssc/api/v1/projects.  This is working as expected as I can execute GETs against various functions with no issue, and if I change the header on the POST I receive a 403.

When I post to the above URL with content I receive the error message "Content was incorrectly formatted (expected application/json; charset=utf-8)", response code 400.  However if I view the request as it executes in Fiddler I can see that the content-type and charset are properly set.

<<incorrectly linked images removed; see below>>

Now my problem is that I don't know if the content is incorrectly formatted, if I'm using the wrong endpoint (Swagger is great but this API is beastly), or if there's a genuine encoding error.  I want to say that it's a payload issue and that I've got it wrong.  This same series of functions works with Fortify for GETs and TFS for GETs/POSTs and they have similar requirements.  At this point I'm flailing around trying to figure that out.

Could someone provide a sample JSON request to add a comment to an issue?  Thanks.

0 Likes
1 Solution

Accepted Solutions
Lieutenant Commander
Lieutenant Commander

New day brings fresh perspective.  Turns out the JSON I was sending was incorrect, it was wrapped in a response object that didn't belong.  To create an issue POST to /issues/{parentId}/comments the following information:

{
  "auditTime": "2018-10-04T18:11:20.162Z",
  "comment": "string",
  "issueId": 0,
  "issueInstanceId": "string",
  "issueName": "string",
  "projectName": "string",
  "projectVersionId": 0,
  "projectVersionName": "string",
  "seqNumber": 0,
  "userName": "string"
}

RTFM. 🙂

View solution in original post

0 Likes
2 Replies
Lieutenant Commander
Lieutenant Commander

There are supposed to be two images of Fiddler in the middle of that message.  I apologize.

Fiddler1.pngFiddler2.png

0 Likes
Lieutenant Commander
Lieutenant Commander

New day brings fresh perspective.  Turns out the JSON I was sending was incorrect, it was wrapped in a response object that didn't belong.  To create an issue POST to /issues/{parentId}/comments the following information:

{
  "auditTime": "2018-10-04T18:11:20.162Z",
  "comment": "string",
  "issueId": 0,
  "issueInstanceId": "string",
  "issueName": "string",
  "projectName": "string",
  "projectVersionId": 0,
  "projectVersionName": "string",
  "seqNumber": 0,
  "userName": "string"
}

RTFM. 🙂

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.