zoukankan      html  css  js  c++  java
  • OpenFeign执行POST请求类型以及Python的requests.post()方法

    使用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 }
    View Code

    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.

  • 相关阅读:
    奶酪工厂
    P1080 国王游戏(非高精版)
    【洛谷P2150】[NOI2015] 寿司晚宴
    【洛谷P3349】[ZJOI2016]小星星
    【洛谷P5785】[SDOI2012]任务安排
    【模板】严格次短路
    【洛谷P3647】[APIO2014]连珠线
    2021.10.27NOIP模拟总结
    【树形DP】CF1016F Road Projects
    2021CSP-S 总结
  • 原文地址:https://www.cnblogs.com/w-honey/p/13041365.html
Copyright © 2011-2022 走看看