使用postman進行接口測試的時候,發現POST請求方式的編碼有3種,具體的編碼方式如下:
A:application/x-www-form-urlencoded ==最常見的post提交數據的方式,以form表單形式提交數據
B:application/json ==以json格式提交數據
C:multipart/form-data ==一般使用來上傳文件(較少用)
使用Postman的時候,按照下圖所示:
自動生成對應的Headers:
requests.post()
在通過requests.post()進行POST請求時,傳入報文的參數有兩個,一個是data,一個是json。常見的form表單可以直接使用data參數進行報文提交,而data的對象則是python中的字典類型。
使用OpenFeign發送Post請求
通過配置,可以把請求方法當作一個service來使用。可以綁定對象,作為傳遞的參數,就跟上面的Body里的各項屬性一樣。
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在feign的類中,首先確定訪問接口的響應形式,是get,還是post,如果是post,是form表單形式的還是json形式的,或者是文件類型的,需要在consumes 里有所體現。
import com.renrenche.guzhiAPI.entity.model.Params; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Service @FeignClient(name = "price-API-flow", url = "${price.configs.price-API-flow.url}") public interface CallPriceFeign { @RequestMapping(method = RequestMethod.POST, path = "/pegasus/v3/init_pricing", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) String callPrice(Params param); }
如果是post的form表單的形式,還需要添加配置,解析form。
// new一個form編碼器,實現支持form表單提交 // 注意這里方法名稱,也就是bean的名稱是什么不重要, // 重要的是返回類型要是 Encoder 並且實現類必須是 FormEncoder 或者其子類 @Bean @Scope("prototype") public Encoder feignFormEncoder() { return new FormEncoder(new SpringEncoder(this.messageConverters)); }
完整的配置代碼如下:

1 import feign.Logger; 2 import feign.codec.Encoder; 3 import feign.form.FormEncoder; 4 import org.springframework.beans.factory.ObjectFactory; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.autoconfigure.http.HttpMessageConverters; 7 import org.springframework.cloud.openfeign.FeignLoggerFactory; 8 import org.springframework.cloud.openfeign.support.SpringEncoder; 9 import org.springframework.context.annotation.Bean; 10 import org.springframework.context.annotation.Configuration; 11 import org.springframework.context.annotation.Scope; 12 13 /** 14 * 開啟INFO級別日志 15 */ 16 @Configuration 17 public class FeignConfig { 18 @Autowired 19 private ObjectFactory<HttpMessageConverters> messageConverters; 20 21 @Bean 22 Logger.Level feignLevel() { 23 return Logger.Level.FULL; 24 } 25 26 @Bean 27 FeignLoggerFactory infoFeignLoggerFactory() { 28 return new InfoFeignLoggerFactory(); 29 } 30 31 // new一個form編碼器,實現支持form表單提交 32 // 注意這里方法名稱,也就是bean的名稱是什么不重要, 33 // 重要的是返回類型要是 Encoder 並且實現類必須是 FormEncoder 或者其子類 34 @Bean 35 @Scope("prototype") 36 public Encoder feignFormEncoder() { 37 return new FormEncoder(new SpringEncoder(this.messageConverters)); 38 } 39 40 }
OpenFeign的日志級別有4種:
-
NONE
, No logging (DEFAULT). -
BASIC
, Log only the request method and URL and the response status code and execution time. -
HEADERS
, Log the basic information along with request and response headers. -
FULL
, Log the headers, body, and metadata for both requests and responses.