2017-08-31 21 views
0

WebClientリクエストを作成する際に間違っていたことを理解するのに問題があります。私は実際のHTTPリクエストがどのようなものか理解したいと思います。私は、APIを構築するためにSpring5の反応性のツールを使用していますSpring WebFlux、WebClient POST交換をどのようにデバッグできますか?

POST /rest/json/send HTTP/1.1 
Host: emailapi.dynect.net 
Cache-Control: no-cache 
Postman-Token: 93e70432-2566-7627-6e08-e2bcf8d1ffcd 
Content-Type: application/x-www-form-urlencoded 

apikey=ABC123XYZ&from=example%40example.com&to=customer1%40domain.com&to=customer2%40domain.com&to=customer3%40domain.com&subject=New+Sale+Coming+Friday&bodytext=You+will+love+this+sale. 

を(例えば、コンソールに生の要求をダンプ)。私はDynの電子メールAPIを使用して電子メールを送信するユーティリティクラスを持っています。 https://help.dyn.com/email-rest-methods-api/sending-api/#postsend

curl --request POST "https://emailapi.dynect.net/rest/json/send" --data "apikey=ABC123XYZ&[email protected]&[email protected]&[email protected]&[email protected]&subject=New Sale Coming Friday&bodytext=You will love this sale." 

:私は、次のコマンドから取られている(org.springframework.web.reactive.function.client.WebClient)これを実現するために、新たなWebClientクラスを使用する

をしたいと思います実際の値でカールして電話をかけると、メールが正しく送信されるので、リクエストが間違っているように感じる。

マイsendコマンド

public Mono<String> send(DynEmailOptions options) 
{ 
    WebClient webClient = WebClient.create(); 
    HttpHeaders headers = new HttpHeaders(); 
    // this line causes unsupported content type exception :(
    // headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 
    Mono<String> result = webClient.post() 
     .uri("https://emailapi.dynect.net/rest/json/send") 
     .headers(headers) 
     .accept(MediaType.APPLICATION_JSON) 
     .body(BodyInserters.fromObject(options)) 
     .exchange() 
     .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); 
    return result; 
} 

マイDynEmailOptionsクラス

import java.util.Collections; 
import java.util.Set; 

public class DynEmailOptions 
{ 
    public String getApikey() 
    { 
     return apiKey_; 
    } 

    public Set<String> getTo() 
    { 
     return Collections.unmodifiableSet(to_); 
    } 

    public String getFrom() 
    { 
     return from_; 
    } 

    public String getSubject() 
    { 
     return subject_; 
    } 

    public String getBodytext() 
    { 
     return bodytext_; 
    } 

    protected DynEmailOptions(
     String apiKey, 
     Set<String> to, 
     String from, 
     String subject, 
     String bodytext 
    ) 
    { 
     apiKey_ = apiKey; 
     to_ = to; 
     from_ = from; 
     subject_ = subject; 
     bodytext_ = bodytext; 
    } 

    private Set<String> to_; 
    private String from_; 
    private String subject_; 
    private String bodytext_; 
    private String apiKey_; 
} 

答えて

3

"であるとして" あなたは現在、右BodyInserterを使用せずに、リクエストボディをシリアル化しようとしています。この場合

、私はあなたがMultiValueMap<String, String>にあなたのDynEmailOptionsオブジェクトをオンにしてからすべきだと思う:

MultiValueMap<String, String> formData = ... 
Mono<String> result = webClient.post() 
       .uri("https://emailapi.dynect.net/rest/json/send") 
       .contentType(MediaType.APPLICATION_FORM_URLENCODED) 
       .accept(MediaType.APPLICATION_JSON) 
       .body(BodyInserters.fromFormData(formData)) 
       .retrieve().bodyToMono(String.class); 
関連する問題