azure-docs-sdk-java/docs-ref-autogen/com.azure.core.http.rest.Re...

199 строки
18 KiB
YAML

### YamlMime:JavaType
uid: "com.azure.core.http.rest.RequestOptions"
fullName: "com.azure.core.http.rest.RequestOptions"
name: "RequestOptions"
nameWithType: "RequestOptions"
summary: "This class contains the options to customize an HTTP request."
inheritances:
- "<xref href=\"java.lang.Object?displayProperty=fullName\" data-throw-if-not-resolved=\"False\" />"
inheritedClassMethods:
- classRef: "java.lang.<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html\">Object</a>"
methodsRef:
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--\">clone</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-\">equals</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--\">finalize</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--\">getClass</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--\">hashCode</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--\">notify</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--\">notifyAll</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--\">toString</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--\">wait</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-\">wait</a>"
- "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-\">wait</a>"
syntax: "public final class **RequestOptions**"
constructors:
- uid: "com.azure.core.http.rest.RequestOptions.RequestOptions()"
fullName: "com.azure.core.http.rest.RequestOptions.RequestOptions()"
name: "RequestOptions()"
nameWithType: "RequestOptions.RequestOptions()"
summary: "Creates a new instance of <xref uid=\"com.azure.core.http.rest.RequestOptions\" data-throw-if-not-resolved=\"false\" data-raw-source=\"RequestOptions\"></xref>."
syntax: "public RequestOptions()"
desc: "Creates a new instance of <xref uid=\"com.azure.core.http.rest.RequestOptions\" data-throw-if-not-resolved=\"false\" data-raw-source=\"RequestOptions\"></xref>."
methods:
- uid: "com.azure.core.http.rest.RequestOptions.addHeader(com.azure.core.http.HttpHeaderName,java.lang.String)"
fullName: "com.azure.core.http.rest.RequestOptions.addHeader(HttpHeaderName header, String value)"
name: "addHeader(HttpHeaderName header, String value)"
nameWithType: "RequestOptions.addHeader(HttpHeaderName header, String value)"
summary: "Adds a header to the HTTP request."
parameters:
- description: "the header key"
name: "header"
type: "<xref href=\"com.azure.core.http.HttpHeaderName?alt=com.azure.core.http.HttpHeaderName&text=HttpHeaderName\" data-throw-if-not-resolved=\"False\" />"
- description: "the header value"
name: "value"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
syntax: "public RequestOptions addHeader(HttpHeaderName header, String value)"
desc: "Adds a header to the HTTP request.\n\nIf a header with the given name exists the `value` is added to the existing header (comma-separated), otherwise a new header is created."
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.addHeader(java.lang.String,java.lang.String)"
fullName: "com.azure.core.http.rest.RequestOptions.addHeader(String header, String value)"
name: "addHeader(String header, String value)"
nameWithType: "RequestOptions.addHeader(String header, String value)"
summary: "Adds a header to the HTTP request."
deprecatedTag: "Use <xref uid=\"com.azure.core.http.rest.RequestOptions.addHeader(com.azure.core.http.HttpHeaderName,java.lang.String)\" data-throw-if-not-resolved=\"false\" data-raw-source=\"#addHeader(HttpHeaderName, String)\"></xref> as it provides better performance."
parameters:
- description: "the header key"
name: "header"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
- description: "the header value"
name: "value"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
syntax: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Deprecated.html\">@Deprecated</a></br>public RequestOptions addHeader(String header, String value)"
desc: "Adds a header to the HTTP request.\n\nIf a header with the given name exists the `value` is added to the existing header (comma-separated), otherwise a new header is created."
hasDeprecatedTag: true
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.addQueryParam(java.lang.String,java.lang.String)"
fullName: "com.azure.core.http.rest.RequestOptions.addQueryParam(String parameterName, String value)"
name: "addQueryParam(String parameterName, String value)"
nameWithType: "RequestOptions.addQueryParam(String parameterName, String value)"
summary: "Adds a query parameter to the request URL."
parameters:
- description: "the name of the query parameter"
name: "parameterName"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
- description: "the value of the query parameter"
name: "value"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
syntax: "public RequestOptions addQueryParam(String parameterName, String value)"
desc: "Adds a query parameter to the request URL. The parameter name and value will be URL encoded. To use an already encoded parameter name and value, call `addQueryParam(\"name\", \"value\", true)`."
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.addQueryParam(java.lang.String,java.lang.String,boolean)"
fullName: "com.azure.core.http.rest.RequestOptions.addQueryParam(String parameterName, String value, boolean encoded)"
name: "addQueryParam(String parameterName, String value, boolean encoded)"
nameWithType: "RequestOptions.addQueryParam(String parameterName, String value, boolean encoded)"
summary: "Adds a query parameter to the request URL, specifying whether the parameter is already encoded."
parameters:
- description: "the name of the query parameter"
name: "parameterName"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
- description: "the value of the query parameter"
name: "value"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
- description: "whether this query parameter is already encoded"
name: "encoded"
type: "<xref href=\"boolean?alt=boolean&text=boolean\" data-throw-if-not-resolved=\"False\" />"
syntax: "public RequestOptions addQueryParam(String parameterName, String value, boolean encoded)"
desc: "Adds a query parameter to the request URL, specifying whether the parameter is already encoded. A value true for this argument indicates that value of <xref uid=\"com.azure.core.annotation.QueryParam.value()\" data-throw-if-not-resolved=\"false\" data-raw-source=\"QueryParam#value()\"></xref> is already encoded hence engine should not encode it, by default value will be encoded."
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.addRequestCallback(java.util.function.Consumer<com.azure.core.http.HttpRequest>)"
fullName: "com.azure.core.http.rest.RequestOptions.addRequestCallback(Consumer<HttpRequest> requestCallback)"
name: "addRequestCallback(Consumer<HttpRequest> requestCallback)"
nameWithType: "RequestOptions.addRequestCallback(Consumer<HttpRequest> requestCallback)"
summary: "Adds a custom request callback to modify the HTTP request before it's sent by the Http<wbr>Client."
parameters:
- description: "the request callback"
name: "requestCallback"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html\">Consumer</a>&lt;<xref href=\"com.azure.core.http.HttpRequest?alt=com.azure.core.http.HttpRequest&text=HttpRequest\" data-throw-if-not-resolved=\"False\" />&gt;"
syntax: "public RequestOptions addRequestCallback(Consumer<HttpRequest> requestCallback)"
desc: "Adds a custom request callback to modify the HTTP request before it's sent by the HttpClient. The modifications made on a RequestOptions object is applied in order on the request."
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.getContext()"
fullName: "com.azure.core.http.rest.RequestOptions.getContext()"
name: "getContext()"
nameWithType: "RequestOptions.getContext()"
summary: "Gets the additional context on the request that is passed during the service call."
syntax: "public Context getContext()"
desc: "Gets the additional context on the request that is passed during the service call."
returns:
description: "The additional context that is passed during the service call."
type: "<xref href=\"com.azure.core.util.Context?alt=com.azure.core.util.Context&text=Context\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.setBody(com.azure.core.util.BinaryData)"
fullName: "com.azure.core.http.rest.RequestOptions.setBody(BinaryData requestBody)"
name: "setBody(BinaryData requestBody)"
nameWithType: "RequestOptions.setBody(BinaryData requestBody)"
summary: "Sets the body to send as part of the HTTP request."
parameters:
- description: "the request body data"
name: "requestBody"
type: "<xref href=\"com.azure.core.util.BinaryData?alt=com.azure.core.util.BinaryData&text=BinaryData\" data-throw-if-not-resolved=\"False\" />"
syntax: "public RequestOptions setBody(BinaryData requestBody)"
desc: "Sets the body to send as part of the HTTP request."
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.setContext(com.azure.core.util.Context)"
fullName: "com.azure.core.http.rest.RequestOptions.setContext(Context context)"
name: "setContext(Context context)"
nameWithType: "RequestOptions.setContext(Context context)"
summary: "Sets the additional context on the request that is passed during the service call."
parameters:
- description: "Additional context that is passed during the service call."
name: "context"
type: "<xref href=\"com.azure.core.util.Context?alt=com.azure.core.util.Context&text=Context\" data-throw-if-not-resolved=\"False\" />"
syntax: "public RequestOptions setContext(Context context)"
desc: "Sets the additional context on the request that is passed during the service call."
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.setHeader(com.azure.core.http.HttpHeaderName,java.lang.String)"
fullName: "com.azure.core.http.rest.RequestOptions.setHeader(HttpHeaderName header, String value)"
name: "setHeader(HttpHeaderName header, String value)"
nameWithType: "RequestOptions.setHeader(HttpHeaderName header, String value)"
summary: "Sets a header on the HTTP request."
parameters:
- description: "the header key"
name: "header"
type: "<xref href=\"com.azure.core.http.HttpHeaderName?alt=com.azure.core.http.HttpHeaderName&text=HttpHeaderName\" data-throw-if-not-resolved=\"False\" />"
- description: "the header value"
name: "value"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
syntax: "public RequestOptions setHeader(HttpHeaderName header, String value)"
desc: "Sets a header on the HTTP request.\n\nIf a header with the given name exists it is overridden by the new `value`."
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
- uid: "com.azure.core.http.rest.RequestOptions.setHeader(java.lang.String,java.lang.String)"
fullName: "com.azure.core.http.rest.RequestOptions.setHeader(String header, String value)"
name: "setHeader(String header, String value)"
nameWithType: "RequestOptions.setHeader(String header, String value)"
summary: "Sets a header on the HTTP request."
deprecatedTag: "Use <xref uid=\"com.azure.core.http.rest.RequestOptions.setHeader(com.azure.core.http.HttpHeaderName,java.lang.String)\" data-throw-if-not-resolved=\"false\" data-raw-source=\"#setHeader(HttpHeaderName, String)\"></xref> as it provides better performance."
parameters:
- description: "the header key"
name: "header"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
- description: "the header value"
name: "value"
type: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/String.html\">String</a>"
syntax: "<a href=\"https://docs.oracle.com/javase/8/docs/api/java/lang/Deprecated.html\">@Deprecated</a></br>public RequestOptions setHeader(String header, String value)"
desc: "Sets a header on the HTTP request.\n\nIf a header with the given name exists it is overridden by the new `value`."
hasDeprecatedTag: true
returns:
description: "the modified RequestOptions object"
type: "<xref href=\"com.azure.core.http.rest.RequestOptions?alt=com.azure.core.http.rest.RequestOptions&text=RequestOptions\" data-throw-if-not-resolved=\"False\" />"
type: "class"
desc: "This class contains the options to customize an HTTP request. <xref uid=\"com.azure.core.http.rest.RequestOptions\" data-throw-if-not-resolved=\"false\" data-raw-source=\"RequestOptions\"></xref> can be used to configure the request headers, query params, the request body, or add a callback to modify all aspects of the HTTP request.\n\nAn instance of fully configured <xref uid=\"com.azure.core.http.rest.RequestOptions\" data-throw-if-not-resolved=\"false\" data-raw-source=\"RequestOptions\"></xref> can be passed to a service method that preconfigures known components of the request like URL, path params etc, further modifying both un-configured, or preconfigured components.\n\nTo demonstrate how this class can be used to construct a request, let's use a Pet Store service as an example. The list of APIs available on this service are [documented in the swagger definition.][]\n\n**Creating an instance of RequestOptions**\n\n```java\nRequestOptions options = new RequestOptions()\n .setBody(BinaryData.fromString(\"{\\\"name\\\":\\\"Fluffy\\\"}\"))\n .addHeader(\"x-ms-pet-version\", \"2021-06-01\");\n```\n\n**Configuring the request with JSON body and making a HTTP POST request**\n\nTo [add a new pet to the pet store][], an HTTP POST call should be made to the service with the details of the pet that is to be added. The details of the pet are included as the request body in JSON format. The JSON structure for the request is defined as follows:\n\n```java\n{\n \"id\": 0,\n \"category\": {\n \"id\": 0,\n \"name\": \"string\"\n },\n \"name\": \"doggie\",\n \"photoUrls\": [\n \"string\"\n ],\n \"tags\": [\n {\n \"id\": 0,\n \"name\": \"string\"\n }\n ],\n \"status\": \"available\"\n }\n```\n\nTo create a concrete request, Json builder provided in javax package is used here for demonstration. However, any other Json building library can be used to achieve similar results.\n\n```java\nJsonArray photoUrls = Json.createArrayBuilder()\n .add(\"https://imgur.com/pet1\")\n .add(\"https://imgur.com/pet2\")\n .build();\n\n JsonArray tags = Json.createArrayBuilder()\n .add(Json.createObjectBuilder()\n .add(\"id\", 0)\n .add(\"name\", \"Labrador\")\n .build())\n .add(Json.createObjectBuilder()\n .add(\"id\", 1)\n .add(\"name\", \"2021\")\n .build())\n .build();\n\n JsonObject requestBody = Json.createObjectBuilder()\n .add(\"id\", 0)\n .add(\"name\", \"foo\")\n .add(\"status\", \"available\")\n .add(\"category\", Json.createObjectBuilder().add(\"id\", 0).add(\"name\", \"dog\"))\n .add(\"photoUrls\", photoUrls)\n .add(\"tags\", tags)\n .build();\n\n String requestBodyStr = requestBody.toString();\n```\n\nNow, this string representation of the JSON request can be set as body of RequestOptions\n\n```java\nRequestOptions options = new RequestOptions()\n .addRequestCallback(request -> request\n // may already be set if request is created from a client\n .setUrl(\"https://petstore.example.com/pet\")\n .setHttpMethod(HttpMethod.POST)\n .setBody(requestBodyStr)\n .setHeader(HttpHeaderName.CONTENT_TYPE, \"application/json\"));\n```\n\n\n[documented in the swagger definition.]: https://petstore.swagger.io/#/pet\n[add a new pet to the pet store]: https://petstore.swagger.io/#/pet/addPet"
metadata: {}
package: "com.azure.core.http.rest"
artifact: com.azure:azure-core:1.52.0