com.ibm.watson.developer_cloud.http.RequestBuilder.post()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(14.0k)|赞(0)|评价(0)|浏览(71)

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

RequestBuilder.post介绍

[英]The POST request method is designed to request that a web server accept the data enclosed in the request message's body for storage. It is often used when uploading a file or submitting a completed web form.
[中]POST request方法旨在请求web服务器接受请求消息正文中包含的数据进行存储。它通常用于上传文件或提交完整的web表单。

代码示例

代码示例来源:origin: com.ibm.watson.developer_cloud/retrieve-and-rank

private RequestBuilder createUploadSolrConfigurationRequest(String solrClusterId, String configName,
  File zippedConfig) {
 final String configPath = createConfigPath(solrClusterId, configName);
 final RequestBuilder requestBuilder = RequestBuilder.post(configPath);
 requestBuilder.body(RequestBody.create(MediaType.parse(HttpMediaType.APPLICATION_ZIP), zippedConfig));
 return requestBuilder;
}

代码示例来源:origin: com.ibm.watson.developer_cloud/retrieve-and-rank

@Override
public ServiceCall<SolrCluster> createSolrCluster(SolrClusterOptions config) {
 final RequestBuilder requestBuilder = RequestBuilder.post(PATH_SOLR_CLUSTERS);
 if (config != null) {
  requestBuilder.bodyContent(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(config),
    HttpMediaType.APPLICATION_JSON);
 }
 return createServiceCall(requestBuilder.build(), ResponseConverterUtils.getObject(SolrCluster.class));
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test post.
 */
@Test
public void testPost() {
 final Request request = RequestBuilder.post(HttpUrl.parse(url)).build();
 assertEquals("POST", request.method());
 assertEquals(url, request.url().toString());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test with nested arrays.
 */
@Test
public void testWithNestedArray() {
 Request request = RequestBuilder.post(HttpUrl.parse(url)).query("foo", new String[] { "bar", "bar2" }).build();
 assertEquals(url + "?foo=bar&foo=bar2", request.url().toString());
 request = RequestBuilder.post(HttpUrl.parse(url)).query("foo", Arrays.asList("bar", "bar2")).build();
 assertEquals(url + "?foo=bar&foo=bar2", request.url().toString());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test with query object array.
 */
@Test
public void testWithQueryObjectArray() {
 final Request request = RequestBuilder.post(HttpUrl.parse(url)).query("foo", "bar", "p2", "p2").build();
 assertEquals(urlWithQuery, request.url().toString());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Create event.
 *
 * The **Events** API can be used to create log entries that are associated with specific queries. For example, you
 * can record which documents in the results set were \"clicked\" by a user and when that click occured.
 *
 * @param createEventOptions the {@link CreateEventOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link CreateEventResponse}
 */
public ServiceCall<CreateEventResponse> createEvent(CreateEventOptions createEventOptions) {
 Validator.notNull(createEventOptions, "createEventOptions cannot be null");
 String[] pathSegments = { "v1/events" };
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
 builder.query(VERSION, versionDate);
 final JsonObject contentJson = new JsonObject();
 contentJson.addProperty("type", createEventOptions.type());
 contentJson.add("data", GsonSingleton.getGson().toJsonTree(createEventOptions.data()));
 builder.bodyJson(contentJson);
 return createServiceCall(builder.build(), ResponseConverterUtils.getObject(CreateEventResponse.class));
}

代码示例来源:origin: com.ibm.watson.developer_cloud/dialog

/**
 * Creates a dialog.
 *
 * @param name The dialog name
 * @param dialogFile The dialog script file
 * @return The created dialog
 * @see Dialog
 */
public ServiceCall<Dialog> createDialog(final String name, final File dialogFile) {
 Validator.isTrue((name != null) && !name.isEmpty(), "name cannot be null or empty");
 Validator.isTrue((dialogFile != null) && dialogFile.exists(), "dialogFile cannot be null or inexistent");
 final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
   .addFormDataPart(FILE, dialogFile.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, dialogFile))
   .addFormDataPart(NAME, name).build();
 final Request request = RequestBuilder.post(PATH_DIALOGS).body(body).build();
 return createServiceCall(request, ResponseConverterUtils.getObject(Dialog.class));
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Create counterexample.
 *
 * Add a new counterexample to a workspace. Counterexamples are examples that have been marked as irrelevant input.
 *
 * This operation is limited to 1000 requests per 30 minutes. For more information, see **Rate limiting**.
 *
 * @param createCounterexampleOptions the {@link CreateCounterexampleOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Counterexample}
 */
public ServiceCall<Counterexample> createCounterexample(CreateCounterexampleOptions createCounterexampleOptions) {
 Validator.notNull(createCounterexampleOptions, "createCounterexampleOptions cannot be null");
 String[] pathSegments = { "v1/workspaces", "counterexamples" };
 String[] pathParameters = { createCounterexampleOptions.workspaceId() };
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments,
   pathParameters));
 builder.query(VERSION, versionDate);
 final JsonObject contentJson = new JsonObject();
 contentJson.addProperty("text", createCounterexampleOptions.text());
 builder.bodyJson(contentJson);
 return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Counterexample.class));
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test with body.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testWithBody() throws IOException {
 final File test = new File("src/test/resources/car.png");
 final Request request =
   RequestBuilder.post(HttpUrl.parse(urlWithQuery))
     .body(RequestBody.create(HttpMediaType.BINARY_FILE, test))
     .build();
 final RequestBody requestedBody = request.body();
 assertEquals(test.length(), requestedBody.contentLength());
 assertEquals(HttpMediaType.BINARY_FILE, requestedBody.contentType());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test with body JSON object.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testWithBodyJsonJsonObject() throws IOException {
 final JsonObject json = new JsonObject();
 json.addProperty("status", "ok");
 final Request request = RequestBuilder.post(HttpUrl.parse(urlWithQuery)).bodyJson(json).build();
 final RequestBody requestedBody = request.body();
 final Buffer buffer = new Buffer();
 requestedBody.writeTo(buffer);
 assertEquals(json.toString(), buffer.readUtf8());
 assertEquals(HttpMediaType.JSON, requestedBody.contentType());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Unregister a callback.
 *
 * Unregisters a callback URL that was previously white-listed with a **Register a callback** request for use with the
 * asynchronous interface. Once unregistered, the URL can no longer be used with asynchronous recognition requests.
 *
 * **See also:** [Unregistering a callback
 * URL](https://cloud.ibm.com/docs/services/speech-to-text/async.html#unregister).
 *
 * @param unregisterCallbackOptions the {@link UnregisterCallbackOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of Void
 */
public ServiceCall<Void> unregisterCallback(UnregisterCallbackOptions unregisterCallbackOptions) {
 Validator.notNull(unregisterCallbackOptions, "unregisterCallbackOptions cannot be null");
 String[] pathSegments = { "v1/unregister_callback" };
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
 builder.query("callback_url", unregisterCallbackOptions.callbackUrl());
 return createServiceCall(builder.build(), ResponseConverterUtils.getVoid());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Create a session.
 *
 * Create a new session. A session is used to send user input to a skill and receive responses. It also maintains the
 * state of the conversation.
 *
 * @param createSessionOptions the {@link CreateSessionOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link SessionResponse}
 */
public ServiceCall<SessionResponse> createSession(CreateSessionOptions createSessionOptions) {
 Validator.notNull(createSessionOptions, "createSessionOptions cannot be null");
 String[] pathSegments = { "v2/assistants", "sessions" };
 String[] pathParameters = { createSessionOptions.assistantId() };
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments,
   pathParameters));
 builder.query(VERSION, versionDate);
 return createServiceCall(builder.build(), ResponseConverterUtils.getObject(SessionResponse.class));
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Identify language.
 *
 * Identifies the language of the input text.
 *
 * @param identifyOptions the {@link IdentifyOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link IdentifiedLanguages}
 */
public ServiceCall<IdentifiedLanguages> identify(IdentifyOptions identifyOptions) {
 Validator.notNull(identifyOptions, "identifyOptions cannot be null");
 String[] pathSegments = { "v3/identify" };
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
 builder.query(VERSION, versionDate);
 builder.bodyContent(identifyOptions.text(), "text/plain");
 return createServiceCall(builder.build(), ResponseConverterUtils.getObject(IdentifiedLanguages.class));
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test with form object array.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testWithFormObjectArray() throws IOException {
 final String body = "foo=bar&test1=test2";
 final Request request = RequestBuilder.post(HttpUrl.parse(urlWithQuery))
   .form("foo", "bar", "test1", "test2")
   .build();
 final RequestBody requestedBody = request.body();
 final Buffer buffer = new Buffer();
 requestedBody.writeTo(buffer);
 assertEquals(body, buffer.readUtf8());
 assertEquals(MediaType.parse(HttpMediaType.APPLICATION_FORM_URLENCODED), requestedBody.contentType());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test build.
 */
@Test
public void testBuild() {
 final String xToken = X_TOKEN;
 final RequestBuilder builder =
   RequestBuilder.post(HttpUrl.parse(urlWithQuery))
     .bodyContent("body1", HttpMediaType.TEXT_PLAIN)
     .header(X_TOKEN, "token1");
 final Request request = builder.build();
 assertEquals("POST", request.method());
 assertEquals("token1", request.header(xToken));
 assertNotNull(builder.toString());
}

代码示例来源:origin: com.ibm.watson.developer_cloud/language-translator

/**
 * Identify language.
 *
 * Identifies the language of the input text.
 *
 * @param identifyOptions the {@link IdentifyOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link IdentifiedLanguages}
 */
public ServiceCall<IdentifiedLanguages> identify(IdentifyOptions identifyOptions) {
 Validator.notNull(identifyOptions, "identifyOptions cannot be null");
 String[] pathSegments = { "v3/identify" };
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
 builder.query(VERSION, versionDate);
 builder.header("X-IBMCloud-SDK-Analytics",
   "service_name=language_translator;service_version=v3;operation_id=identify");
 builder.bodyContent(identifyOptions.text(), "text/plain");
 return createServiceCall(builder.build(), ResponseConverterUtils.getObject(IdentifiedLanguages.class));
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Test with content string.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testWithContentString() throws IOException {
 final String body = "test2";
 final Request request = RequestBuilder.post(HttpUrl.parse(urlWithQuery))
   .bodyContent(body, HttpMediaType.TEXT_PLAIN).build();
 final RequestBody requestedBody = request.body();
 final Buffer buffer = new Buffer();
 requestedBody.writeTo(buffer);
 assertEquals(body, buffer.readUtf8());
 assertEquals(HttpMediaType.TEXT, requestedBody.contentType());
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Refresh an IAM token using a refresh token. Also updates internal managed IAM token information.
 *
 * @return the new access token
 */
private String refreshToken() {
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(url, new String[0]));
 builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_FORM_URLENCODED);
 builder.header(HttpHeaders.AUTHORIZATION, DEFAULT_AUTHORIZATION);
 FormBody formBody = new FormBody.Builder()
   .add(GRANT_TYPE, REFRESH_GRANT_TYPE)
   .add(REFRESH_TOKEN, tokenData.getRefreshToken())
   .build();
 builder.body(formBody);
 tokenData = callIamApi(builder.build());
 return tokenData.getAccessToken();
}

代码示例来源:origin: com.ibm.watson.developer_cloud/core

/**
 * Refresh an IAM token using a refresh token. Also updates internal managed IAM token information.
 *
 * @return the new access token
 */
private String refreshToken() {
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(url, new String[0]));
 builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_FORM_URLENCODED);
 builder.header(HttpHeaders.AUTHORIZATION, DEFAULT_AUTHORIZATION);
 FormBody formBody = new FormBody.Builder()
   .add(GRANT_TYPE, REFRESH_GRANT_TYPE)
   .add(REFRESH_TOKEN, tokenData.getRefreshToken())
   .build();
 builder.body(formBody);
 tokenData = callIamApi(builder.build());
 return tokenData.getAccessToken();
}

代码示例来源:origin: watson-developer-cloud/java-sdk

/**
 * Request an IAM token using an API key. Also updates internal managed IAM token information.
 *
 * @return the new access token
 */
private String requestToken() {
 RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(url, new String[0]));
 builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_FORM_URLENCODED);
 builder.header(HttpHeaders.AUTHORIZATION, DEFAULT_AUTHORIZATION);
 FormBody formBody = new FormBody.Builder()
   .add(GRANT_TYPE, REQUEST_GRANT_TYPE)
   .add(API_KEY, apiKey)
   .add(RESPONSE_TYPE, CLOUD_IAM)
   .build();
 builder.body(formBody);
 tokenData = callIamApi(builder.build());
 return tokenData.getAccessToken();
}

相关文章