Skip to content

Commit dfeb63f

Browse files
author
dksimpson
committed
Refresh Bing Video Search REST API quickstarts
1 parent 279f4e0 commit dfeb63f

File tree

7 files changed

+116
-108
lines changed

7 files changed

+116
-108
lines changed

articles/cognitive-services/Bing-Video-Search/quickstarts/csharp.md

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

1616
# Quickstart: Search for videos using the Bing Video Search REST API and C#
1717

18-
Use this quickstart to make your first call to the Bing Video Search API and view a search result from the JSON response. This simple C# application sends an HTTP video search query to the API, and displays the response. While this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
18+
Use this quickstart to make your first call to the Bing Video Search API. This simple C# application sends an HTTP video search query to the API, and displays the JSON response. Although this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
1919

2020
The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/dotnet/Search/BingVideoSearchv7.cs) with additional error handling, features, and code annotations.
2121

2222
## Prerequisites
2323
* Any edition of [Visual Studio 2017 or later](https://www.visualstudio.com/downloads/).
2424
* The [Json.NET](https://www.newtonsoft.com/json) framework, available as a NuGet package.
25-
* If you are using Linux/MacOS, this application can be run using [Mono](https://www.mono-project.com/).
25+
* If you're using Linux/MacOS, this application can be run using [Mono](https://www.mono-project.com/).
2626

2727
[!INCLUDE [cognitive-services-bing-video-search-signup-requirements](../../../../includes/cognitive-services-bing-video-search-signup-requirements.md)]
2828

2929
## Create and initialize a project
3030

31-
1. Create a new console solution in Visual Studio. Then add the following namespaces into the main code file.
31+
1. Create a new console solution in Visual Studio. Then, add the following namespaces to the main code file:
3232

3333
```csharp
3434
using System;
@@ -38,17 +38,17 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
3838
using System.Collections.Generic;
3939
```
4040

41-
2. Add variables for your subscription key, endpoint, and search term. `uriBase` can be the global endpoint below, or the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
41+
2. Add variables for your subscription key, endpoint, and search term. 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.
4242

4343
```csharp
4444
const string accessKey = "enter your key here";
4545
const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/videos/search";
4646
const string searchTerm = "kittens";
4747
```
4848

49-
### Create a struct to format the Bing Video Search API response
49+
## Create a struct to format the Bing Video Search API response
5050

51-
1. Define a `SearchResult` struct to contain the image search results, and JSON header information.
51+
Define a `SearchResult` struct to contain the image search results, and JSON header information.
5252

5353
```csharp
5454
struct SearchResult
@@ -60,19 +60,20 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
6060

6161
## Create and handle a video search request
6262

63-
Create a method named `BingVideoSearch` to perform the call to the API, and set the return type to the `SearchResult` struct created earlier. In the method, perform the following steps:
63+
1. Create a method named `BingVideoSearch` to perform the call to the API, and set the return type to the `SearchResult` struct created previously.
6464

65-
1. Construct the URI for the search request. Note that the search term toSearch must be formatted before being appended to the string.
65+
Add code to this method in the steps that follow.
6666

67-
```csharp
68-
67+
1. Construct the URI for the search request. Format the search term `toSearch` before you append it to the string.
68+
69+
```csharp
6970
static SearchResult BingVideoSearch(string toSearch){
7071

7172
var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(toSearch);
7273
//...
7374
```
7475

75-
2. Perform the web request by adding your key to the `Ocp-Acpim-Subscription-Key` header, and using a `HttpWebResponse` object to store the API response. Then use a `StreamReader` to get the JSON string.
76+
2. Perform the web request by adding your key to the `Ocp-Acpim-Subscription-Key` header, and using a `HttpWebResponse` object to store the API response. Then, use a `StreamReader` to get the JSON string.
7677

7778
```csharp
7879
//...
@@ -85,7 +86,7 @@ Create a method named `BingVideoSearch` to perform the call to the API, and set
8586

8687
## Process the result
8788

88-
1. Create the search result object, and extract the Bing HTTP headers. Then return the `searchResult` object.
89+
1. Create the search result object, and extract the Bing HTTP headers. Then, return the `searchResult` object.
8990

9091
```csharp
9192
var searchResult = new SearchResult();
@@ -101,7 +102,7 @@ Create a method named `BingVideoSearch` to perform the call to the API, and set
101102
return searchResult;
102103
```
103104

104-
2. You can then print the response.
105+
2. Print the response.
105106

106107
```csharp
107108
Console.WriteLine(result.jsonResult);

articles/cognitive-services/Bing-Video-Search/quickstarts/java.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-video-search
1111
ms.topic: quickstart
12-
ms.date: 12/09/2019
12+
ms.date: 05/19/2020
1313
ms.author: aahi
1414
---
1515
# Quickstart: Search for videos using the Bing Video Search REST API and Java
1616

17-
Use this quickstart to make your first call to the Bing Video Search API and view a search result from the JSON response. This simple Java application sends an HTTP video search query to the API, and displays the response. While this application is written in Java, the API is a RESTful Web service compatible with most programming languages. The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingVideoSearchv7.java) with additional error handling, features, and code annotations.
17+
Use this quickstart to make your first call to the Bing Video Search API. This simple Java application sends an HTTP video search query to the API and displays the JSON response.
18+
19+
Although this application is written in Java, the API is a RESTful Web service compatible with most programming languages. The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingVideoSearchv7.java) with additional error handling, features, and code annotations.
1820

1921
## Prerequisites
2022

21-
* The [Java Development Kit(JDK)](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
23+
* The [Java Development Kit (JDK)](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
2224

2325
* The [Gson library](https://github.com/google/gson)
2426

@@ -27,7 +29,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
2729

2830
## Create and initialize a project
2931

30-
1. Create a new Java project in your favorite IDE or editor, and import the following libraries.
32+
1. Create a new Java project in your favorite IDE or editor, and import the following libraries:
3133

3234
```java
3335
import java.net.*;
@@ -54,7 +56,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
5456
}
5557
```
5658

57-
3. Create a new method named `SearchVideos()` with variables for your API endpoint host and path, your subscription key, and a search term. It will return a `SearchResults` object. `host` can be the global endpoint below, or the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
59+
3. Create a new method named `SearchVideos()` with variables for your API endpoint host and path, your subscription key, and search term. This method returns a `SearchResults` object. You can use the value of `host` 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.
5860

5961
```java
6062
public static SearchResults SearchVideos (String searchQuery) throws Exception {
@@ -67,41 +69,41 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
6769

6870
## Construct and send the search request
6971

70-
1. In `SearchVideos()`, perform the following steps:
72+
In the `SearchVideos()` method, perform the following steps:
7173

72-
1. construct the URL for your request by combining your API host, path, and encoding your search query. Then use `openConnection()` to create a connection, and add your subscription key to the `Ocp-Apim-Subscription-Key` header.
74+
1. Construct the URL for your request by combining your API host, path, and encoded search query. Then, use `openConnection()` to create a connection, and add your subscription key to the `Ocp-Apim-Subscription-Key` header.
7375

74-
```java
75-
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchQuery, "UTF-8"));
76-
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
77-
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
78-
```
76+
```java
77+
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchQuery, "UTF-8"));
78+
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
79+
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
80+
```
7981

80-
2. Get the response from the API and store the JSON string.
82+
2. Get the response from the API and store the JSON string.
8183

82-
```java
83-
InputStream stream = connection.getInputStream();
84-
String response = new Scanner(stream).useDelimiter("\\A").next();
85-
```
84+
```java
85+
InputStream stream = connection.getInputStream();
86+
String response = new Scanner(stream).useDelimiter("\\A").next();
87+
```
8688

87-
3. Use `getHeaderFields();` to extract the HTTP headers from the response, and store the Bing-related ones in the `results` object. Then close the stream and return the result.
89+
3. Use `getHeaderFields()` to extract the HTTP headers from the response, and store the Bing-related ones in the `results` object. Then, close the stream and return the result.
8890

89-
```java
90-
// extract Bing-related HTTP headers
91-
Map<String, List<String>> headers = connection.getHeaderFields();
92-
for (String header : headers.keySet()) {
93-
if (header == null) continue; // may have null key
94-
if (header.startsWith("BingAPIs-") || header.startsWith("X-MSEdge-")) {
95-
results.relevantHeaders.put(header, headers.get(header).get(0));
96-
}
97-
}
98-
stream.close();
99-
return results;
100-
```
91+
```java
92+
// extract Bing-related HTTP headers
93+
Map<String, List<String>> headers = connection.getHeaderFields();
94+
for (String header : headers.keySet()) {
95+
if (header == null) continue; // may have null key
96+
if (header.startsWith("BingAPIs-") || header.startsWith("X-MSEdge-")) {
97+
results.relevantHeaders.put(header, headers.get(header).get(0));
98+
}
99+
}
100+
stream.close();
101+
return results;
102+
```
101103

102104
## Format the response
103105

104-
1. Create a method named `prettify()` to format the response returned from the Bing Video API. Use the Gson library's `JsonParser` to take in a JSON string and convert it into an object. Then use `GsonBuilder()` and `toJson()` to create the formatted string.
106+
1. Create a method named `prettify()` to format the response returned from the Bing Video API. Use the Gson library's `JsonParser` to convert a JSON string to an object. Then, use `GsonBuilder()` and `toJson()` to create the formatted string.
105107
106108
```java
107109
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
@@ -115,18 +117,18 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
115117
116118
## Send the request and print the response
117119
118-
1. In the main method of your application, call `SearchVideos` with your search term. you can then print the HTTP headers stored in the response, as well as the JSON string returned by the API.
120+
In the main method of your application, call `SearchVideos` with your search term. Then, print the HTTP headers stored in the response and the JSON string returned by the API.
119121
120-
```java
121-
public static void main (String[] args) {
122+
```java
123+
public static void main (String[] args) {
122124
123-
SearchResults result = SearchVideos(searchTerm);
124-
//print the Relevant HTTP Headers
125-
for (String header : result.relevantHeaders.keySet())
126-
System.out.println(header + ": " + result.relevantHeaders.get(header));
127-
System.out.println(prettify(result.jsonResponse));
128-
}
129-
```
125+
SearchResults result = SearchVideos(searchTerm);
126+
//print the Relevant HTTP Headers
127+
for (String header : result.relevantHeaders.keySet())
128+
System.out.println(header + ": " + result.relevantHeaders.get(header));
129+
System.out.println(prettify(result.jsonResponse));
130+
}
131+
```
130132
131133
## JSON response
132134

articles/cognitive-services/Bing-Video-Search/quickstarts/nodejs.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-video-search
1111
ms.topic: quickstart
12-
ms.date: 12/09/2019
12+
ms.date: 05/19/2020
1313
ms.author: aahi
1414
---
1515
# Quickstart: Search for videos using the Bing Video Search REST API and Node.js
1616

17-
Use this quickstart to make your first call to the Bing Video Search API and view a search result from the JSON response. This simple JavaScript application sends an HTTP video search query to the API, and displays the response. While this application is written in JavaScript and uses Node.js, the API is a RESTful Web service compatible with most programming languages. The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/nodejs/Search/BingVideoSearchv7.js) with additional error handling, and code annotations.
17+
Use this quickstart to make your first call to the Bing Video Search API. This simple JavaScript application sends an HTTP video search query to the API, and displays the JSON response.
18+
19+
Although this application is written in JavaScript and uses Node.js, the API is a RESTful Web service compatible with most programming languages. The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/nodejs/Search/BingVideoSearchv7.js) with additional error handling, and code annotations.
1820

1921
## Prerequisites
2022

21-
* [Node.js](https://nodejs.org/en/download/)
23+
* [Node.js](https://nodejs.org/en/download/).
2224

23-
* The Request module for JavaScript
24-
* You can install this module using `npm install request`
25+
* The Request module for JavaScript. Install this module by using `npm install request`.
2526

2627
[!INCLUDE [cognitive-services-bing-video-search-signup-requirements](../../../../includes/cognitive-services-bing-video-search-signup-requirements.md)]
2728

@@ -34,7 +35,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
3435
let https = require('https');
3536
```
3637

37-
2. Create variables for your API endpoint, subscription key, and your search term. `host` can be the global endpoint below, or 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 search term. You can use the value of `host` 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.
3839

3940
```javascript
4041
let subscriptionKey = 'enter key here';
@@ -45,7 +46,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
4546

4647
## Create a response handler
4748

48-
1. Create a function called `response_handler` to take a JSON response from the API. Create a variable for the response body. Append the response when a `data` flag is received, using `response.on()`.
49+
1. Create a function called `response_handler` to take a JSON response from the API. Create a variable for the response body. Append the response when a `data` flag is received by using `response.on()`.
4950

5051
```javascript
5152
let response_handler = function (response) {
@@ -56,23 +57,23 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
5657
};
5758
```
5859

59-
1. When `end` is signaled, use `response.on()` to store the bing-related headers (beginning with `bingapis` or `x-msedge-`). Then parse the JSON using `JSON.parse()`, convert it to a string with `JSON.stringify()`, and print it.
60-
61-
```javascript
62-
response.on('end', function () {
63-
for (var header in response.headers)
64-
// header keys are lower-cased by Node.js
65-
if (header.startsWith("bingapis-") || header.startsWith("x-msedge-"))
66-
console.log(header + ": " + response.headers[header]);
67-
body = JSON.stringify(JSON.parse(body), null, ' ');
68-
//JSON Response body
69-
console.log(body);
70-
});
71-
```
60+
1. When `end` is signaled, use `response.on()` to store the bing-related headers (beginning with `bingapis` or `x-msedge-`). Parse the JSON using `JSON.parse()`, convert it to a string with `JSON.stringify()`, and print it.
61+
62+
```javascript
63+
response.on('end', function () {
64+
for (var header in response.headers)
65+
// header keys are lower-cased by Node.js
66+
if (header.startsWith("bingapis-") || header.startsWith("x-msedge-"))
67+
console.log(header + ": " + response.headers[header]);
68+
body = JSON.stringify(JSON.parse(body), null, ' ');
69+
//JSON Response body
70+
console.log(body);
71+
});
72+
```
7273

7374
## Create and send the search request
7475

75-
1. Create a function called `bing_video_search()`. Add the parameters for your request including your host name, and headers. Encode your search term and append it to your path parameter with the `?q=` parameter. Then send the request with `req.end()`.
76+
Create a function called `bing_video_search()`. Add the parameters for your request including your host name, and headers. Encode your search term and append it to your path parameter with the `?q=` parameter. Then, send the request with `req.end()`.
7677

7778
```javascript
7879
let bing_video_search = function (search_term) {

0 commit comments

Comments
 (0)