Hello, how can I send an image in a post request ?...
# suitescript
c
Hello, how can I send an image in a post request ? I found this https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4567628658.html So I tried this code :
Copy code
var headerObj = {
   'Authorization': 'Bearer ' + tk,
   'Accept': 'application/json',
   'Content-Type': 'multipart/form-data; boundary=' + boundary
};
var bodyObj = boundary + 
'\r\nContent-Disposition: form-data; name="file"; filename="test.png"\r\nContent-Type:image/png\r\n\r\n' +
myFile.getContents() + '\r\n' + boundary;

var response = <http://https.post|https.post>({
     url: myUrl,
     headers: headerObj,
     body: bodyObj
});
But it does not work and I get a 422 error with message "the key 'file' is required to upload. Please check your body content" Any idea ?
b
depends on the api you are sending to
the example you shared is if the api you are working with uses multipart data
c
@battk it is working fine when I use Postman so the multipart data is accepted by the API
b
i can tell from the code that its unlikely that you have actually matched the content from postman
your boundaries are off
you will want to start out by logging your body and comparing it to the one from postman
also, netsuite is fairly unfriendly to binary content, you will have trouble getting the file data correct unless the api supports base 64 for the content transfer encoding
c
@battk what do you mean by "your boundaries are off" ?
c
To me, my body in suitescript
Copy code
var bodyObj = boundary + 
'\r\nContent-Disposition: form-data; name="file"; filename="test.png"\r\nContent-Type:image/png\r\n\r\n' +
myFile.getContents() + '\r\n' + boundary;
looks like the postman body. Isn't it ?
b
log the actual string in netsuite
c
the problem comes from the content, right ? It is what you said about base64 encoding data ?
b
there are multiple problems
the first of which you arent generating valid multipart data
c
Here is my netsuite log for my body :
b
use a diff tool to compare your request to postman's
c
like what tool ?
b
c
So, I tried to write the request with node fetch and here is the FormData that has been sent in the body, and the headers of the request :
In nodejs everything worked fine, so I suppose that the problem is the base64 encoding in Netsuite... I don't understand your clues about the boundaries... So I would appreciate some help. If it's not possible to upload the file because of the encoding, is there a way to convert it into buffer in netsuite ?
b
you are building the body of your request incorrectly
postman and node are doing it correctly
you want to compare your request body to the body generated by those apps
if you arent thorough enough to find the differences yourself, then use a diff tool to find lines that are different
you will later have problem with the content of the actual file, but i would recommend fixing one problem at a time
c
I modified the filename. Was it the error ?
b
your goal is to make the request body generated by your code match that of postman
i personally cant tell from images of text, so you are the one who needs to use the diff tool