How to post multipart/form-data?

470 Views Asked by At

I'm creating an extension that automates procedures within Codenvy by using the REST API they provide. However, I'm having trouble with a multipart/form-data POST request. Specically, creating a new factory.

This is my ajax/jquery call:

$.ajax({
    type: 'POST',
    url: 'https://codenvy.com/api/factory',
    data: formData,
    processData: false,
    contentType: false,
    success:function(data) {
      callback();
    },
    error:function(e) {
      console.log(e);
    }
  });

But I get a 409 error with the message:

No factory URL information found in 'factoryURL' section of multipart/form-data

I already posted on Codenvy forums, but the only useful thing I got was a curl command

POST http://domain.com/api/factory?token=$token -H 'Content-Type: multipart/form-data' -F 'factoryUrl={$JSONofFactoryConfig}'

rfc2388 says something about a name field, but I have no idea how to convert the curl name field into an ajax request.

How should I go about adding this "factoryUrl" field to my POST request?

2

There are 2 best solutions below

0
On

It looks like you are missing factoryUrl in your payload. It should be:

factoryUrl={json_with_project_config}
0
On

I resolved this. I knew that I was missing the factoryUrl portion in my payload, I just didn't know how to put it in my payload. The following code did the job:

var formData = new FormData();
formData.append('factoryUrl', JSON.stringify(jsonObject));
// Send ajax post request