You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dataweave数据库结果转CSV异常:API返回JSON而非CSV求助

Fix: DataWeave Outputs JSON Instead of CSV in Mule API

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 Accept header to text/csv or application/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-Type and value to application/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 default application/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:37:32