Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

netsuite - suitescript: POST an image form the cabinet multipart/form-data

I am trying to post an image form the cabinet to an API

the API is rejected call with Content-Type": "multipart/form-data:

{ httpCode: "405", httpMessage: "Method Not Allowed", message: "PUT, POST require an input body.", errorClass: "Exception" }

the API is rejected call without Content-Type": "multipart/form-data:

{ httpCode: "400", httpMessage: "Bad Request", message: "JSON Input was invalid. Error Message: Syntax error", errorClass: "InvalidArgumentException" }

the current code is:

function ItemImageCreation(){
var itemId = 4;
var payload;

var StringUrl = "https://someURL";
var boundary = '--' + uuidv4();
var files = file.load(1056); // getting the file
var fileContents = files.getContents(); // getting the content
var decodedStr = fromBaseUTF(fileContents); // conversion to Base64
var form_data = "{"description": "Test Image",
"ordering": 1
}";

// add the data field
payload = "
" + boundary + "
"
  + 'Content-Disposition: form-data; name="data"

'
  + form_data + "
"
  + boundary + "
"
  + 'Content-Disposition: form-data; name="image"
'
  + 'Content-Type: image/jpeg

'
  + decodedStr + "
"
  + boundary + "--

";
log.debug("payload", payload);
var Header = {"Authorization": "Bearer " + token,
                        "Content-Type": "multipart/form-data; boundary=" + boundary
                        };
try {
  var response = https.post({
    url: StringUrl,
    body: payload,
    headers: Header
  });
  var newSFID = JSON.parse(response.body);
  log.debug("Item Image creation", newSFID);
} catch (e) {
  log.error({ title: 'Failed to submit file', details: (e.message || e.toString()) + (e.getStackTrace ? (' 
 
' + e.getStackTrace().join(' 
')) : '') });
  log.error('ERROR Item Image Creation', JSON.stringify(e));
}

}

using postman, the image is correctly sent: enter image description here enter image description here

I am using a scheduled script, do you see what is wrong or is there a way to know what is send by netsuite?

question from:https://stackoverflow.com/questions/65921100/suitescript-post-an-image-form-the-cabinet-multipart-form-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is an answer here that covers this: In NetSuite with SuiteScript 2.0 unable to send a file with HTTP POST request with a content-type multipart/form-data

what you missing is the Content-Transfer-Encoding header and you should be getting the content as Base64 so you shouldn't need to convert from UTF16 ( I could be wrong on that but I've never needed to)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...