Skip to content

Commit fce98a0

Browse files
author
dksimpson
committed
Edits
1 parent 6f8b33d commit fce98a0

File tree

6 files changed

+111
-112
lines changed

6 files changed

+111
-112
lines changed

articles/cognitive-services/bing-visual-search/quickstarts/csharp.md

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-visual-search
1111
ms.topic: quickstart
12-
ms.date: 05/19/2020
12+
ms.date: 05/21/2020
1313
ms.author: scottwhi
1414
---
1515

@@ -37,7 +37,7 @@ This quickstart demonstrates how to upload an image to the Bing Visual Search AP
3737
using System.Collections.Generic;
3838
```
3939

40-
2. Add variables for your subscription key, endpoint, and path to the image you want to upload. You can use the value of `uriBase` in the following code for the global endpoint, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource:
40+
2. Add variables for your subscription key, endpoint, and path to the image you want to upload. For the `uriBase` value, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource:
4141

4242
```csharp
4343
const string accessKey = "<my_subscription_key>";
@@ -189,79 +189,79 @@ This quickstart demonstrates how to upload an image to the Bing Visual Search AP
189189

190190
If you use `HttpClient`, you can use the `MultipartFormDataContent` class to build the form data. Use the following sections of code to replace the corresponding methods in the previous example.
191191

192-
Replace the `Main()` method with this code:
193-
194-
```csharp
195-
static void Main()
196-
{
197-
try
198-
{
199-
Console.OutputEncoding = System.Text.Encoding.UTF8;
200-
201-
if (accessKey.Length == 32)
202-
{
203-
if (IsImagePathSet(imagePath))
204-
{
205-
var filename = GetImageFileName(imagePath);
206-
Console.WriteLine("Getting image insights for image: " + filename);
207-
var imageBinary = GetImageBinary(imagePath);
208-
209-
var boundary = string.Format(BoundaryTemplate, Guid.NewGuid());
210-
var json = BingImageSearch(imageBinary, boundary, uriBase, accessKey);
211-
212-
Console.WriteLine("\nJSON Response:\n");
213-
Console.WriteLine(JsonPrettyPrint(json));
214-
}
215-
}
216-
else
217-
{
218-
Console.WriteLine("Invalid Bing Visual Search API subscription key!");
219-
Console.WriteLine("Please paste yours into the source code.");
220-
}
221-
222-
Console.Write("\nPress Enter to exit ");
223-
Console.ReadLine();
224-
}
225-
catch (Exception e)
226-
{
227-
Console.WriteLine(e.Message);
228-
}
229-
}
230-
```
231-
232-
Replace the `BingImageSearch()` method with this code:
233-
234-
```csharp
235-
/// <summary>
236-
/// Calls the Bing visual search endpoint and returns the JSON response.
237-
/// </summary>
238-
static string BingImageSearch(byte[] image, string boundary, string uri, string subscriptionKey)
239-
{
240-
var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
241-
requestMessage.Headers.Add("Ocp-Apim-Subscription-Key", accessKey);
242-
243-
var content = new MultipartFormDataContent(boundary);
244-
content.Add(new ByteArrayContent(image), "image", "myimage");
245-
requestMessage.Content = content;
246-
247-
var httpClient = new HttpClient();
248-
249-
Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
250-
HttpResponseMessage httpResponse = httpRequest.Result;
251-
HttpStatusCode statusCode = httpResponse.StatusCode;
252-
HttpContent responseContent = httpResponse.Content;
253-
254-
string json = null;
255-
256-
if (responseContent != null)
257-
{
258-
Task<String> stringContentsTask = responseContent.ReadAsStringAsync();
259-
json = stringContentsTask.Result;
260-
}
261-
262-
return json;
263-
}
264-
```
192+
1. Replace the `Main()` method with the following code:
193+
194+
```csharp
195+
static void Main()
196+
{
197+
try
198+
{
199+
Console.OutputEncoding = System.Text.Encoding.UTF8;
200+
201+
if (accessKey.Length == 32)
202+
{
203+
if (IsImagePathSet(imagePath))
204+
{
205+
var filename = GetImageFileName(imagePath);
206+
Console.WriteLine("Getting image insights for image: " + filename);
207+
var imageBinary = GetImageBinary(imagePath);
208+
209+
var boundary = string.Format(BoundaryTemplate, Guid.NewGuid());
210+
var json = BingImageSearch(imageBinary, boundary, uriBase, accessKey);
211+
212+
Console.WriteLine("\nJSON Response:\n");
213+
Console.WriteLine(JsonPrettyPrint(json));
214+
}
215+
}
216+
else
217+
{
218+
Console.WriteLine("Invalid Bing Visual Search API subscription key!");
219+
Console.WriteLine("Please paste yours into the source code.");
220+
}
221+
222+
Console.Write("\nPress Enter to exit ");
223+
Console.ReadLine();
224+
}
225+
catch (Exception e)
226+
{
227+
Console.WriteLine(e.Message);
228+
}
229+
}
230+
```
231+
232+
2. Replace the `BingImageSearch()` method with the following code:
233+
234+
```csharp
235+
/// <summary>
236+
/// Calls the Bing visual search endpoint and returns the JSON response.
237+
/// </summary>
238+
static string BingImageSearch(byte[] image, string boundary, string uri, string subscriptionKey)
239+
{
240+
var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
241+
requestMessage.Headers.Add("Ocp-Apim-Subscription-Key", accessKey);
242+
243+
var content = new MultipartFormDataContent(boundary);
244+
content.Add(new ByteArrayContent(image), "image", "myimage");
245+
requestMessage.Content = content;
246+
247+
var httpClient = new HttpClient();
248+
249+
Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
250+
HttpResponseMessage httpResponse = httpRequest.Result;
251+
HttpStatusCode statusCode = httpResponse.StatusCode;
252+
HttpContent responseContent = httpResponse.Content;
253+
254+
string json = null;
255+
256+
if (responseContent != null)
257+
{
258+
Task<String> stringContentsTask = responseContent.ReadAsStringAsync();
259+
json = stringContentsTask.Result;
260+
}
261+
262+
return json;
263+
}
264+
```
265265

266266
## Next steps
267267

articles/cognitive-services/bing-visual-search/quickstarts/go.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-visual-search
1111
ms.topic: quickstart
12-
ms.date: 05/19/2020
12+
ms.date: 05/21/2020
1313
ms.author: aahi
1414
---
1515

@@ -184,7 +184,7 @@ func BuildFormDataEnd(batNum string) string{
184184
```
185185
## Add image bytes to POST body
186186

187-
This code segment creates the POST request that contains image data:
187+
The following code creates the POST request that contains image data:
188188

189189
```go
190190
func createRequestBody(fileName string, batchNumber string) (*bytes.Buffer, string) {

articles/cognitive-services/bing-visual-search/quickstarts/java.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-visual-search
1111
ms.topic: quickstart
12-
ms.date: 05/19/2020
12+
ms.date: 05/21/2020
1313
ms.author: scottwhi
1414
---
1515

@@ -48,7 +48,7 @@ Use this quickstart to make your first call to the Bing Visual Search API. This
4848
import org.apache.http.impl.client.HttpClientBuilder;
4949
```
5050

51-
2. Create variables for your API endpoint, subscription key, and the path to your image. You can use the value of `endpoint` in the following code for the global endpoint, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource:
51+
2. Create variables for your API endpoint, subscription key, and the path to your image. For the `endpoint` value, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource:
5252

5353
```java
5454
static String endpoint = "https://api.cognitive.microsoft.com/bing/v7.0/images/visualsearch";

articles/cognitive-services/bing-visual-search/quickstarts/nodejs.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-visual-search
1111
ms.topic: quickstart
12-
ms.date: 05/19/2020
12+
ms.date: 05/21/2020
1313
ms.author: scottwhi
1414
---
1515

@@ -35,7 +35,7 @@ Use this quickstart to make your first call to the Bing Visual Search API. This
3535
var fs = require('fs');
3636
```
3737

38-
2. Create variables for your API endpoint, subscription key, and the path to your image. You can use the value of `baseUri` in the following code for the global endpoint, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource:
38+
2. Create variables for your API endpoint, subscription key, and the path to your image. For the `baseUri` value, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource:
3939

4040
```javascript
4141
var baseUri = 'https://api.cognitive.microsoft.com/bing/v7.0/images/visualsearch';
@@ -53,25 +53,25 @@ Use this quickstart to make your first call to the Bing Visual Search API. This
5353

5454
## Construct and send the search request
5555

56-
When you upload a local image, the form data must include the `Content-Disposition` header. Set its `name` parameter to "image", and set the `filename` parameter to any string. The contents of the form include the binary data of the image. The maximum image size you can upload is 1 MB.
56+
1. When you upload a local image, the form data must include the `Content-Disposition` header. Set its `name` parameter to "image", and set the `filename` parameter to any string. The contents of the form include the binary data of the image. The maximum image size you can upload is 1 MB.
5757

58-
```
59-
--boundary_1234-abcd
60-
Content-Disposition: form-data; name="image"; filename="myimagefile.jpg"
58+
```
59+
--boundary_1234-abcd
60+
Content-Disposition: form-data; name="image"; filename="myimagefile.jpg"
6161

62-
ÿØÿà JFIF ÖÆ68g-¤CWŸþ29ÌÄøÖ‘º«™æ±èuZiÀ)"óÓß°Î= ØJ9á+*G¦...
62+
ÿØÿà JFIF ÖÆ68g-¤CWŸþ29ÌÄøÖ‘º«™æ±èuZiÀ)"óÓß°Î= ØJ9á+*G¦...
6363

64-
--boundary_1234-abcd--
65-
```
64+
--boundary_1234-abcd--
65+
```
6666
67-
1. Create a new **FormData** object using `FormData()`, and append your image path to it by using `fs.createReadStream()`:
67+
2. Create a new **FormData** object with `FormData()`, and append your image path to it by using `fs.createReadStream()`:
6868
6969
```javascript
7070
var form = new FormData();
7171
form.append("image", fs.createReadStream(imagePath));
7272
```
7373
74-
2. Use the request library to upload the image, and call `requestCallback()` to print the response. Add your subscription key to the request header:
74+
3. Use the request library to upload the image, and call `requestCallback()` to print the response. Add your subscription key to the request header:
7575
7676
```javascript
7777
form.getLength(function(err, length){

articles/cognitive-services/bing-visual-search/quickstarts/python.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-visual-search
1111
ms.topic: quickstart
12-
ms.date: 05/19/2020
12+
ms.date: 05/21/2020
1313
ms.author: scottwhi
1414
---
1515

@@ -31,7 +31,7 @@ Use this quickstart to make your first call to the Bing Visual Search API. This
3131
import requests, json
3232
```
3333

34-
2. Create variables for your subscription key, endpoint, and the path to the image you're uploading. You can use the value of `BASE_URI` in the following code for the global endpoint, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
34+
2. Create variables for your subscription key, endpoint, and the path to the image you're uploading. For the value of `BASE_URI`, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
3535

3636
```python
3737

@@ -65,27 +65,27 @@ Use this quickstart to make your first call to the Bing Visual Search API. This
6565

6666
## Parse the JSON response
6767

68-
1. Create a method called `print_json()` to take in the API response, and print the JSON:
68+
Create a method called `print_json()` to take in the API response, and print the JSON:
6969

70-
```python
71-
def print_json(obj):
72-
"""Print the object as json"""
73-
print(json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')))
74-
```
70+
```python
71+
def print_json(obj):
72+
"""Print the object as json"""
73+
print(json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')))
74+
```
7575

7676
## Send the request
7777

78-
1. Use `requests.post()` to send a request to the Bing Visual Search API. Include the string for your endpoint, header, and file information. Print `response.json()` with `print_json()`:
78+
Use `requests.post()` to send a request to the Bing Visual Search API. Include the string for your endpoint, header, and file information. Print `response.json()` with `print_json()`:
7979

80-
```python
81-
try:
82-
response = requests.post(BASE_URI, headers=HEADERS, files=file)
83-
response.raise_for_status()
84-
print_json(response.json())
80+
```python
81+
try:
82+
response = requests.post(BASE_URI, headers=HEADERS, files=file)
83+
response.raise_for_status()
84+
print_json(response.json())
8585

86-
except Exception as ex:
87-
raise ex
88-
```
86+
except Exception as ex:
87+
raise ex
88+
```
8989

9090
## Next steps
9191

articles/cognitive-services/bing-visual-search/quickstarts/ruby.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-visual-search
1111
ms.topic: quickstart
12-
ms.date: 05/19/2020
12+
ms.date: 05/21/2020
1313
ms.author: aahi
1414
---
1515

@@ -26,14 +26,13 @@ This quickstart uses the Ruby programming language to call Bing Visual Search an
2626

2727
## Project and required modules
2828

29-
Create a new Ruby project in your IDE or editor. Import `net/http`, `uri` , and `json` to handle the JSON text of results. Import the `base64` library, which is used to encode the file name string:
29+
Create a new Ruby project in your IDE or editor. Import `net/http`, `uri` , and `json` to handle the JSON text of results. Import the `base64` library, which encodes the file name string:
3030

3131
```
3232
require 'net/https'
3333
require 'uri'
3434
require 'json'
3535
require 'base64'
36-
3736
```
3837

3938
## Define variables
@@ -62,7 +61,7 @@ end
6261

6362
## Form data for POST request
6463

65-
1. The image data to POST is enclosed by leading and trailing boundaries. The following functions set the boundaries:
64+
1. Enclose the image data to POST by leading and trailing boundaries. The following functions set the boundaries:
6665

6766
```
6867
def BuildFormDataStart(batNum, fileName)
@@ -107,7 +106,7 @@ request.body = post_body.join
107106

108107
## Request and response
109108

110-
Ruby sends the request and gets the response with the following line of code:
109+
Ruby sends the request and gets the response with the following code:
111110

112111
```
113112
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|

0 commit comments

Comments
 (0)