Dataweave数据库结果转CSV异常:API返回JSON而非CSV求助
Hey there, I’ve dealt with this exact frustration before—you set everything up to return CSV in DataWeave, but Postman/SoapUI keeps spitting out JSON. Let’s walk through the most common fixes:
1. Your Request’s Accept Header Is Overriding the Output
Most API clients like Postman default to sending Accept: application/json in requests. Mule prioritizes the client’s requested content type over your DataWeave output declaration.
Quick fixes:
- In Postman/SoapUI, go to the Headers tab and add/update the
Acceptheader totext/csvorapplication/csv, then re-send the request. - For a permanent fix (so clients don’t have to set this header), add a Set Header component right after your Transform step. Set the header name to
Content-Typeand value toapplication/csv—this forces the response to be CSV regardless of the client’s request headers.
2. The Transform Component’s Output MIME Type Isn’t Configured
Sometimes just adding output application/csv in your DataWeave code isn’t enough, especially in older Mule Runtime versions.
Check this:
- Open your Transform component in Mule Studio.
- Go to the Output section in the properties panel.
- Make sure the MIME Type is explicitly set to
application/csv(not left as the defaultapplication/json).
3. Verify Your DataWeave & Payload Structure
Double-check that your payload has the exact field names you’re referencing. For example, if your database returns NUM (all caps) instead of Num, your id: $.Num will return null—but that shouldn’t switch the output to JSON. Still, it’s worth logging the payload to confirm:
%dw 2.0 output application/csv --- log("Raw DB Payload: ", payload) map { name : $.NAME, id: $.Num, age: $.age }
Check the Mule logs to see if the payload matches what you expect.
4. Ensure No Subsequent Components Overwrite the Response
Make sure there are no other components after your Transform step that might change the output format. For example, an error handler that returns JSON, or a Set Payload component that’s accidentally set to JSON.
Example Working Flow
Here’s a reliable pipeline to ensure CSV output:
- Database Query component (fetches rows)
- Transform component (with your DataWeave code + MIME Type set to CSV)
- Set Header component (Content-Type: application/csv)
- HTTP Listener Response (finishes the request)
内容的提问来源于stack exchange,提问作者Nadeem

