How to accept any multipart file (image/pdf/csv) from postman and create the same file into SFTP location in Mule 4? - mule

When I tried this usecase for some MIME types the content is coming in payload.parts[0].content, for some MIME types the content is coming in payload.parts.file.content. How to create the file in the same MIME type as how we received from postman in SFTP?

Quite not sure in what ways you have used and tested the both expression. Please find the screenshot below. I have added different format of datas in single request. If you use mutiple format in single request and wanting to extract individually. Use payload.parts[0].content and payload.parts[1].content
Note: This expression ideally mean whatever you add (form-data) in postman body request can be extract using index.
Example: if you have added content-type: application/xml in the below picture 3rd row, should be using payload.parts[2].content
If you're using single request payload.parts.file.content can be used.
Once after the request reached HTTP/HTTPS ( source listener), use dataweave as below to convert binary in to the defined structure data
%dw 2.0
output multipart/form-data
---
payload
After extracting the contents using the expressions, you can write in to SFTP (Screenshot) here I have stored in Vars (based on your logic)

Related

How to set content-type in Rest Client step

I'm using the Rest Client as below,
But every time I have the same error message
HTTP Error 400. The request has an invalid header name.
When I use the same config in postman client, (Content-Type:application/x-www-form-urlencoded) I've success message.
What I have to do ?
This might be somewhat reviving an old question, however:
In the headers tab on the Rest Client step the field column refers to a data field in your input stream to this step. The Name refers to the Name of the header. In the screenshot you've added you're trying to add a header called ${CONTENT_TYPE} with the value as contained in your stream field content-type.
Your first step appears to be a data grid. That grid needs to be supplying a field that you reference as your content-type header. If it can't come from that grid you need to inject a step (something like Add Constant) that will add the content-type header.
Hope it helps.

How to POST a XML file in jmeter body instead of a physical file

I'm using JMeter 3.2.
My requirement is to read an XML file from the disk, replace some tags with dynamic values to ensure each thread sends a unique xml file upload (NOT SOAP Request). The following code in JSR223 sampler works perfectly fine when I try to upload the newfile through POST using a http sampler with ${newfilename} file text/xml.
import org.apache.commons.io.FileUtils;
try {
String content = FileUtils.readFileToString(new File("E:/test.xml"));
content = content.replaceAll("SUB_ID", "${__UUID}");
content = content.replaceAll("ABN_ID", "${empabn}");
content = content.replaceAll("EMPNAME", "${empname}");
vars.put("content", content);
FileUtils.writeStringToFile(new File("E:/testnew${empname}.xml"), content);
}
catch (Throwable ex) {
log.info("What happened?", ex);
throw ex;
}
Instead of writing again to the disk and uploading again, how can I send the contents of string 'content' as part of request body? I have looked at many posts that talk about the input output streams but they are confusing. When I try to send just ${content} in body, the application throws following error:
HTTP Status 500 - Could not write JSON: Name is null (through reference chain: com.xxx.xxx.datafile.rest.DataFileResponse["validationStatus"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Name is null (through reference chain:
Appreciate your help.
Multipart POST requests which are being used for files upload are different from normal POST requests hence there no possibility to simply substitute the file with the generated in-memory string.
You need to replicate the request exactly as it would be send by JMeter or real browser and manually populate each part starting from defining boundary using the HTTP Header Manager and ending up with the creation of Content-Disposition and specify your file contents there.
A little hint: you don't need to generate/substitute values for each call, it is enough to replace them once and JMeter will substitute them on its own given you use __eval() and __FileToString() functions combination.
You can check out Testing REST API File Uploads in JMeter for an example of creation a relatively complex file upload request, in your case it will be easier but still tricky.

Unable to upload multipart file in karate, Required request part '' not present

ActualAPIRequest OutputFromKarate
Trying to upload a json file for an api using karate. Since api takes multipart input i am passing multipart configurations in karate.
But Required request part 'inputData' not present error is coming. Is there any solution for this please?
I have attached actual input and result from karate screenshot for reference
Just make sure that the data type of inputData and maybe swaggerFile is JSON. Looks like you are sending a string.
Please refer to this section of the doc: https://github.com/intuit/karate#type-conversion
If the server does not like the charset being sent for each multipart, try * configure charset = null

Jmeter - use csv for setting http header

I want to set header for each request.
header is -
"token:value"
For every request this header value will be different and I have a csv file having all tokens listed there. Now, like we use csv for POST data values, I want to use this csv file to set header for each request.
CSV file -
"token1,token2,token3"
How to do this in jmeter ?
To achieve your goal, you should do following for each request:
Add HTTP Header Manager:
2 . Add CSV Data Set Config configured like this:
the idea is pretty good, if the referer field is widely used.
I'm not sure that you can use CSV data sets, because it reads ONE line for each user/ thread.
In a situation similar to yours, I had to use a __CSVread function, that allows to manually control if and when to read the next line.
Refer to: "CSVRead">http://jmeter.apache.org/usermanual/functions.html#_CSVRead"
If request the same but header changes, then you obviously need CSV Data Set Config element. Example of usage you can find in %JMETER_HOME%/bin/examples. If requests are different then I'm not sure it makes sens to use CSV file.

after receiving soap response modify xml and send to anther service using http adapter

while using http adapter I need to call first service that return XML,
after receiving the response I want to change values and send back to anther service,
how can I do it ?
do http adapter has json to xml function ?
WL adapter will automatically convert XML to JSON for you, however it doesn't have any manual JSON<->XML conversion APIs.
In your case possible solution might be to retrieve XML as plaintext by supplying returnedContentType:"plain" in invocation options. Alter whatever you need using regex/string replace. Use resulting string in 2nd procedure invocation as post body.
Alternatively, you can use 3rd party library to parse/convert/do whatever you need with XML, e.g. http://www.json.org/java/ (more info about how to use it in your adapter - http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v506/04_08_Using_Java_in_adapters.pdf)
After checking number of solutions, I state the the http result will be a plain text,
then made a call to java function sending the xml as String, and used
javax.xml to hold and alter the XML.
XPath to retrieve the correct node using org.w3c.dom.*
Hope this will help you too.