VMware Cloud Community
pizzle85
Expert
Expert
Jump to solution

REST POST form-data content type

I'm attempting to POST through a REST API that has a content type of form-data from vRO. Does anyone have any idea how i need to format the body of the request in vRO? At this point im just building a JSON object then stringifying the object and sending it off.

{key1:value1,key2:value2}

var body = new Object();

body.api_key = apiKey;

body.AssignedSupportLevel = assignedSupportLevel;

body.Category = category;

body.CustomersUFID = customersUFID;

body.Description = description;

body.LastModifiedByEmail = lastModifiedByEmail;

body.OwnedByTeam = ownedByTeam;

body.Portfolio = portfolio;

body.Priority = priority;

body.Service = service;

body.Source = source;

body.Subcategory = subcategory;

body.Summary = summary;

var request = restHost.createRequest("POST", "/myitapi/v1/tickets", JSON.stringify(body));

request.contentType = "multipart/form-data";

return request.execute();

Reply
0 Kudos
1 Solution

Accepted Solutions
pizzle85
Expert
Expert
Jump to solution

Thanks for the reply Ilian. I was able to format the request body as specified in the RFCs as shown below. I also wrote an action to do the formatting for me which i attached. Hopefully this will help someone else out.

var body = "--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" +

"Content-Disposition: form-data; name=\"api_key\"\r\n\r\n" +

apiKey + "\r\n" +

"--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" +

"Content-Disposition: form-data; name=\"AssignedSupportLevel\"\r\n\r\n" +

assignedSupportLevel + "\r\n" +

"--WebKitFormBoundary7MA4YWxkTrZu0gW--"

The action has two expectations as described in the description.

  1. You must use the boundary as described in the details in your content-type for the request.
  2. The action assumes that all form data is of the Content-Disposition: form-data type.

View solution in original post

Reply
0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Read the following RFC https://www.ietf.org/rfc/rfc1867.txt , in particular section 3.3 (use of multipart/form-data) and 6 (Examples).

So to properly format the request body you should know the structure of the form you want to POST to. Consult the documentation of your REST API.

pizzle85
Expert
Expert
Jump to solution

Thanks for the reply Ilian. I was able to format the request body as specified in the RFCs as shown below. I also wrote an action to do the formatting for me which i attached. Hopefully this will help someone else out.

var body = "--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" +

"Content-Disposition: form-data; name=\"api_key\"\r\n\r\n" +

apiKey + "\r\n" +

"--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" +

"Content-Disposition: form-data; name=\"AssignedSupportLevel\"\r\n\r\n" +

assignedSupportLevel + "\r\n" +

"--WebKitFormBoundary7MA4YWxkTrZu0gW--"

The action has two expectations as described in the description.

  1. You must use the boundary as described in the details in your content-type for the request.
  2. The action assumes that all form data is of the Content-Disposition: form-data type.
Reply
0 Kudos