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

如何在Spring Boot REST中发送带多请求头与纯文本请求体的POST请求?

Fixing Your POST Request Implementation

Let me walk you through correcting this code—there are a few key issues that are keeping your request from working as intended, whether you need a plain text request body (as you mentioned) or the originally attempted multipart/form-data request.

Key Issues in Your Original Code

  • You constructed a proper multipart body but passed the hardcoded string "parameters" into HttpEntity instead of your built body variable.
  • You're setting both a manual Basic Auth header and adding a BasicAuthorizationInterceptor—this is redundant, pick one approach.
  • If your goal is a plain text request body, the entire multipart/form-data configuration is unnecessary.

Option 1: Plain Text Request Body (As Stated in Your Goal)

If you need to send a plain text payload, simplify the code like this:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.support.BasicAuthorizationInterceptor;

import java.util.Collections;

@RequestMapping(value = "/run", method = RequestMethod.POST)
public ResponseEntity<String> runReport() {
    RestTemplate restTemplate = new RestTemplate();
    String targetUrl = "requestURL"; // Replace with your actual endpoint URL
    String username = "username";
    String password = "password";

    // Use interceptor for clean Basic Auth handling (no manual Base64 needed)
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));

    // Configure headers for plain text request
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); // Adjust based on expected response type

    // Your plain text request body content
    String plainTextBody = "Insert your plain text content here";

    HttpEntity<String> requestEntity = new HttpEntity<>(plainTextBody, headers);
    ResponseEntity<String> response = restTemplate.exchange(targetUrl, HttpMethod.POST, requestEntity, String.class);

    return response;
}

Option 2: Corrected Multipart/Form-Data Request (If That's What You Actually Need)

If your target endpoint expects multipart/form-data (as your original code tried to implement), fix the body reference and header consistency:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.support.BasicAuthorizationInterceptor;

import java.util.Collections;

@RequestMapping(value = "/run", method = RequestMethod.POST)
public ResponseEntity<String> runReport() {
    RestTemplate restTemplate = new RestTemplate();
    String targetUrl = "requestURL"; // Replace with your actual endpoint URL
    String username = "username";
    String password = "password";

    // Use interceptor for Basic Auth (cleaner than manual header setup)
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));

    String outputFormatType = "pdf";
    String boundary = "Boundary_1_1153447573_1465550731355";
    
    // Build the multipart body with a boundary that matches the header
    String multipartBody = "--" + boundary + "\n" +
            "Content-Type: application/json\n" +
            "Content-Disposition: form-data; name=\"ReportRequest\"\n\n" +
            "{\"byPassCache\":true,\"flattenXML\":false,\"attributeFormat\":\""+outputFormatType+"\"}\n" +
            "--" + boundary + "--";

    // Configure headers with the exact boundary string
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("multipart/form-data; boundary=\"" + boundary + "\""));
    headers.setAccept(Collections.singletonList(MediaType.MULTIPART_FORM_DATA));

    // Pass the correct multipart body into HttpEntity
    HttpEntity<String> requestEntity = new HttpEntity<>(multipartBody, headers);
    ResponseEntity<String> response = restTemplate.exchange(targetUrl, HttpMethod.POST, requestEntity, String.class);

    return response;
}

Quick Notes

  • For plain text requests, always set Content-Type to text/plain to match the payload format.
  • For multipart requests, ensure the boundary string in the header matches exactly what's used in the body.
  • Stick to one auth method (the interceptor is cleaner) to avoid conflicts or redundant code.

内容的提问来源于stack exchange,提问作者Pralay Das

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:22:06