io.micronaut.http.HttpRequest.POST()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(231)

本文整理了Java中io.micronaut.http.HttpRequest.POST()方法的一些代码示例,展示了HttpRequest.POST()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.POST()方法的具体详情如下:
包路径:io.micronaut.http.HttpRequest
类名称:HttpRequest
方法名:POST

HttpRequest.POST介绍

[英]Return a MutableHttpRequest that executes an HttpMethod#POST request for the given URI.
[中]返回对给定URI执行HttpMethod#POST请求的可变HttpRequest。

代码示例

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Return a {@link MutableHttpRequest} that executes an {@link HttpMethod#POST} request for the given URI.
 *
 * @param uri  The URI
 * @param body The body of the request (content type defaults to {@link MediaType#APPLICATION_JSON}
 * @param <T>  The body type
 * @return The {@link MutableHttpRequest} instance
 * @see HttpRequestFactory
 */
static <T> MutableHttpRequest<T> POST(URI uri, T body) {
  return POST(uri.toString(), body);
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
public O invoke(FunctionDefinition definition, I input, Argument<O> outputType) {
  Optional<URI> opt = definition.getURI();
  if (!opt.isPresent()) {
    throw new FunctionNotFoundException(definition.getName());
  } else {
    URI uri = opt.get();
    HttpRequest request;
    if (input == null) {
      request = HttpRequest.GET(uri.toString());
    } else {
      request = HttpRequest.POST(uri.toString(), input);
    }
    if (input != null && ClassUtils.isJavaLangType(input.getClass())) {
      ((MutableHttpRequest) request).contentType(MediaType.TEXT_PLAIN_TYPE);
    }
    Class<O> outputJavaType = outputType.getType();
    if (ClassUtils.isJavaLangType(outputJavaType)) {
      ((MutableHttpRequest) request).accept(MediaType.TEXT_PLAIN_TYPE);
    }
    if (Publishers.isConvertibleToPublisher(outputJavaType)) {
      Publisher publisher = httpClient.retrieve(request, outputType.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT));
      return ConversionService.SHARED.convert(publisher, outputType).orElseThrow(() ->
        new FunctionExecutionException("Unsupported Reactive type: " + outputJavaType)
      );
    } else {
      return (O) httpClient.toBlocking().retrieve(request, outputType);
    }
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testPostInvalidFormData() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
    RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());

    // tag::postform[]
    Map<String, String> data = new LinkedHashMap<>();
    data.put("title", "The Stand");
    data.put("pages", "notnumber");
    data.put("url", "noturl");
    Flowable<HttpResponse<Book>> call = client.exchange(
        POST("/binding/book", data)
            .contentType(MediaType.APPLICATION_FORM_URLENCODED),
        Book.class
    );
    // end::postform[]

    thrown.expect(HttpClientResponseException.class);
    thrown.expectMessage(CoreMatchers.startsWith("Failed to convert argument [pages] for value [notnumber]"));

    HttpResponse<Book> response = call.blockingFirst();

    embeddedServer.stop();
    client.stop();
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

POST("/greet", new Message("Hello John")), // <1>
    Message.class // <2>
);

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testPostRequestWithString() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::poststring[]
  Flowable<HttpResponse<String>> call = client.exchange(
      POST("/hello", "Hello John") // <1>
        .contentType(MediaType.TEXT_PLAIN_TYPE)
        .accept(MediaType.TEXT_PLAIN_TYPE), // <2>
      String.class // <3>
  );
  // end::poststring[]
  HttpResponse<String> response = call.blockingFirst();
  Optional<String> message = response.getBody(String.class); // <2>
  // check the status
  assertEquals(
      HttpStatus.CREATED,
      response.getStatus() // <3>
  );
  // check the body
  assertTrue(message.isPresent());
  assertEquals(
      "Hello John",
      message.get()
  );
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testPostWithURITemplate() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::posturitemplate[]
  Flowable<HttpResponse<Book>> call = client.exchange(
      POST("/amazon/book/{title}", new Book("The Stand")),
      Book.class
  );
  // end::posturitemplate[]
  HttpResponse<Book> response = call.blockingFirst();
  Optional<Book> message = response.getBody(Book.class); // <2>
  // check the status
  assertEquals(
      HttpStatus.CREATED,
      response.getStatus() // <3>
  );
  // check the body
  assertTrue(message.isPresent());
  assertEquals(
      "The Stand",
      message.get().getTitle()
  );
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

POST("/amazon/book/{title}", new Book("The Stand"))
.contentType(MediaType.APPLICATION_FORM_URLENCODED),
Book.class

代码示例来源:origin: io.micronaut/micronaut-http

/**
 * Return a {@link MutableHttpRequest} that executes an {@link HttpMethod#POST} request for the given URI.
 *
 * @param uri  The URI
 * @param body The body of the request (content type defaults to {@link MediaType#APPLICATION_JSON}
 * @param <T>  The body type
 * @return The {@link MutableHttpRequest} instance
 * @see HttpRequestFactory
 */
static <T> MutableHttpRequest<T> POST(URI uri, T body) {
  return POST(uri.toString(), body);
}

代码示例来源:origin: org.ctoolkit.agent/ctoolkit-agent-core

MutableHttpRequest<ImportBatch> post = HttpRequest.POST( new URI( "/api/v1/imports" ), importBatch );
httpClient.retrieve( post ).blockingFirst();

代码示例来源:origin: micronaut-projects/micronaut-examples

@Test
  @Ignore
  public void testMailSend() throws Exception {
    //String requestBody = "{\"cc\": [\"sergio.delamo@softamo.com\"],\"recipient\": \"sergio.delamo@softamo.com\", \"subject\": \"Interested in Pet\", \"replyTo\": \"sergio.delamo@softamo.com\", \"htmlBody\": \"Body html\", \"bcc\": [\"sergio.delamo@softamo.com\"]}";
    String requestBody = "{\"recipient\": \"sergio.delamo@softamo.com\"}";
    HttpResponse rsp = client.toBlocking().exchange(HttpRequest.POST("/v1/mail/send",requestBody));
    assertEquals(200, rsp.getStatus().getCode());
  }
}

相关文章