Skip to content

Commit 763e382

Browse files
authored
Merge pull request #115070 from dksimpson/DKS-US1712683-bing-news-search
Refresh Bing News Search REST API quickstarts
2 parents 250d2f5 + e4b8289 commit 763e382

File tree

8 files changed

+152
-141
lines changed

8 files changed

+152
-141
lines changed

articles/cognitive-services/Bing-News-Search/csharp.md

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ manager: nitinme
88
ms.service: cognitive-services
99
ms.subservice: bing-news-search
1010
ms.topic: quickstart
11-
ms.date: 12/12/2019
11+
ms.date: 05/22/2020
1212
ms.author: aahi
1313
ms.custom: seodec2018
1414
---
1515

1616
# Quickstart: Search for news using C# and the Bing News Search REST API
1717

18-
Use this quickstart to make your first call to the Bing News Search API and view the JSON response. This simple C# application sends a news search query to the API, and displays the response. The full code to this sample can be found on [GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/dotnet/Search/BingNewsSearchv7.cs).
18+
Use this quickstart to make your first call to the Bing News Search API. This simple C# application sends a news search query to the API, and displays the JSON response.
1919

20-
While this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
20+
Although this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
21+
22+
The full code to this sample can be found on [GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/dotnet/Search/BingNewsSearchv7.cs).
2123

2224
## Prerequisites
2325

2426
* Any edition of [Visual Studio 2017 or later](https://www.visualstudio.com/downloads/).
2527
* The [Json.NET](https://www.newtonsoft.com/json) framework, available as a NuGet package.
26-
* If you are using Linux/MacOS, this application can be run using [Mono](https://www.mono-project.com/).
28+
* If you're using Linux/MacOS, you can run this application by using [Mono](https://www.mono-project.com/).
2729

2830
[!INCLUDE [cognitive-services-bing-news-search-signup-requirements](../../../includes/cognitive-services-bing-news-search-signup-requirements.md)]
2931

3032
## Create and initialize a project
3133

32-
1. create a new C# console solution in Visual Studio. Then add the following namespaces into the main code file.
34+
1. Create a new C# console solution in Visual Studio. Then, add the following namespaces to the main code file:
3335

3436
```csharp
3537
using System;
@@ -39,30 +41,33 @@ While this application is written in C#, the API is a RESTful Web service compat
3941
using System.Collections.Generic;
4042
```
4143

42-
2. Create variables for the API endpoint, your subscription key, and search term. You can use the global endpoint below, or the [custom subdomain](../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
44+
2. Create variables for the API endpoint, your subscription key, and search term. 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.
4345

4446
```csharp
4547
const string accessKey = "enter key here";
4648
const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/news/search";
4749
const string searchTerm = "Microsoft";
4850
```
49-
## Create a struct to format the Bing News Search response
51+
52+
## Create a struct to format the Bing News Search response
5053

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

53-
```csharp
54-
struct SearchResult
55-
{
56-
public String jsonResult;
57-
public Dictionary<String, String> relevantHeaders;
58-
}
59-
```
56+
```csharp
57+
struct SearchResult
58+
{
59+
public String jsonResult;
60+
public Dictionary<String, String> relevantHeaders;
61+
}
62+
```
6063

6164
## Create and handle a news search request
6265

63-
Create a method named `BingNewsSearch` 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:
66+
1. Create a method named `BingNewsSearch()` to call the API, and set the return type to the `SearchResult` struct created previously.
67+
68+
Add code to this method in the steps that follow.
6469

65-
1. Construct the URI for the search request. Note that the search term `toSearch` must be formatted before being appended to the string.
70+
1. Construct the URI for the search request. The `toSearch` search term must be formatted before it's appended to the string.
6671

6772
```csharp
6873
static SearchResult BingNewsSearch(string toSearch){
@@ -71,7 +76,7 @@ Create a method named `BingNewsSearch` to perform the call to the API, and set t
7176
//...
7277
```
7378

74-
2. Perform the web request and get the response as a JSON string.
79+
1. Perform the web request and get the response as a JSON string.
7580

7681
```csharp
7782
WebRequest request = WebRequest.Create(uriQuery);
@@ -80,7 +85,7 @@ Create a method named `BingNewsSearch` to perform the call to the API, and set t
8085
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
8186
```
8287

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

8590
```csharp
8691
// Create the result object for return
@@ -101,16 +106,16 @@ Create a method named `BingNewsSearch` to perform the call to the API, and set t
101106

102107
## Process the response
103108

104-
1. In the main method, call `BingNewsSearch()` and store the returned response. Then deserialize the JSON into an object. You can then view the values of the response.
109+
In the main method, call `BingNewsSearch()` and store the returned response. Then, deserialize the JSON into an object where you can view the values of the response.
105110

106-
```csharp
107-
SearchResult result = BingNewsSearch(searchTerm);
108-
//deserialize the JSON response
109-
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result.jsonResult);
110-
Console.WriteLine(jsonObj["value"][0])
111-
```
111+
```csharp
112+
SearchResult result = BingNewsSearch(searchTerm);
113+
//deserialize the JSON response
114+
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result.jsonResult);
115+
Console.WriteLine(jsonObj["value"][0])
116+
```
112117

113-
## JSON Response
118+
## Example JSON response
114119

115120
A successful response is returned in JSON, as shown in the following example:
116121

articles/cognitive-services/Bing-News-Search/go.md

Lines changed: 13 additions & 14 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: 12/16/2019
12+
ms.date: 05/22/2020
1313
ms.author: aahi
1414
---
1515

@@ -18,15 +18,14 @@ ms.author: aahi
1818
This quickstart uses the Go language to call the Bing News Search API. The results include names and URLs of news sources identified by the query string.
1919

2020
## Prerequisites
21-
* Install the [Go binaries](https://golang.org/dl/)
22-
* Install the go-spew library for it pretty printer to display results
23-
* Install this library: `$ go get -u https://github.com/davecgh/go-spew`
21+
* Install the [Go binaries](https://golang.org/dl/).
22+
* Install the go-spew library to use a deep pretty printer to display the results. Use this command to install the library: `$ go get -u https://github.com/davecgh/go-spew`.
2423

2524
[!INCLUDE [cognitive-services-bing-news-search-signup-requirements](../../../includes/cognitive-services-bing-news-search-signup-requirements.md)]
2625

2726
## Create a project and import libraries
2827

29-
Create a new Go project in your IDE or editor. Then import `net/http` for requests, `ioutil` to read the response, and `encoding/json` to handle the JSON text of results. The go-spew library is needed to parse JSON.
28+
Create a new Go project in your IDE or editor. Then, import `net/http` for requests, `ioutil` to read the response, `encoding/json` to handle the JSON text of results, and the `go-spew` library to parse the JSON results.
3029

3130
```go
3231
package main
@@ -41,9 +40,9 @@ import (
4140

4241
```
4342

44-
## Create a struct to format the News search results
43+
## Create a struct to format the news search results
4544

46-
The `NewsAnswer` struct formats the data provided in the response. The response JSON is multilevel and quite complex. The following implementation covers the essentials.
45+
The `NewsAnswer` struct formats the data provided in the response JSON, which is multilevel and complex. The following implementation covers the essentials:
4746

4847
```go
4948
// This struct formats the answer provided by the Bing News Search API.
@@ -83,7 +82,7 @@ type NewsAnswer struct {
8382

8483
## Declare the main function and define variables
8584

86-
The following code declares the main function and assigns required variables. Confirm that the endpoint is correct and replace the `token` value with a valid subscription key from your Azure account. You can use the global endpoint below, or the [custom subdomain](../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
85+
The following code declares the main function and assigns the required variables. Confirm that the endpoint is correct, and then replace the `token` value with a valid subscription key from your Azure account. 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.
8786

8887
```go
8988
func main() {
@@ -104,7 +103,7 @@ func main() {
104103

105104
## Query and header
106105

107-
Add the query string and access key header
106+
Add the query string and access key header.
108107

109108
```go
110109
// Add the query to the request.
@@ -117,9 +116,9 @@ req.Header.Add("Ocp-Apim-Subscription-Key", token)
117116

118117
```
119118

120-
## Get request
119+
## GET request
121120

122-
Create the client and send the Get request.
121+
Create the client and send the GET request.
123122

124123
```go
125124
// Instantiate a client.
@@ -135,7 +134,7 @@ if err != nil {
135134

136135
## Send the request
137136

138-
Send the request and read results using `ioutil`.
137+
Send the request and read the results by using `ioutil`.
139138

140139
```go
141140
resp, err := client.Do(req)
@@ -156,7 +155,7 @@ if err != nil {
156155

157156
## Handle the response
158157

159-
The `Unmarshall` function extracts information from the JSON text returned by the News Search API. Then you can display nodes from the results using the `go-spew` pretty printer.
158+
The `Unmarshall` function extracts information from the JSON text returned by the Bing News Search API. Then, display nodes from the results with the `go-spew` pretty printer.
160159

161160
```go
162161
// Create a new answer object
@@ -177,7 +176,7 @@ spew.Dump(result.Name, result.URL)
177176

178177
## Results
179178

180-
The results contain name and URL of each result.
179+
The following output contains the name and URL of each result:
181180

182181
```
183182
(string) (len=91) "Cognitive Services Market: Global Industry Analysis and Opportunity Assessment, 2019 - 2025"

articles/cognitive-services/Bing-News-Search/java.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-news-search
1111
ms.topic: quickstart
12-
ms.date: 12/16/2019
12+
ms.date: 05/22/2020
1313
ms.author: aahi
1414
ms.custom: seodec2018
1515
---
1616

1717
# Quickstart: Perform a news search using Java and the Bing News Search REST API
1818

19-
Use this quickstart to make your first call to the Bing News Search API and view the JSON response. This simple Java application sends a news search query to the API, and displays the response.
19+
Use this quickstart to make your first call to the Bing News Search API. This simple Java application sends a news search query to the API, and displays the JSON response.
2020

21-
While this application is written in Java, the API is a RESTful Web service compatible with most programming languages.
21+
Although this application is written in Java, the API is a RESTful Web service compatible with most programming languages.
2222

23-
The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingNewsSearchv7.java)
23+
The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingNewsSearchv7.java).
2424

2525
## Prerequisites
2626

27-
* The [Java Development Kit(JDK) 7 or 8](https://aka.ms/azure-jdks)
28-
29-
* The [Gson library](https://github.com/google/gson)
27+
* The [Java Development Kit (JDK) 7 or 8](https://aka.ms/azure-jdks).
28+
* The [Gson library](https://github.com/google/gson).
3029

3130

3231
[!INCLUDE [cognitive-services-bing-news-search-signup-requirements](../../../includes/cognitive-services-bing-news-search-signup-requirements.md)]
3332

3433
## Create and initialize a project
3534

36-
1. Create a new Java project in your favorite IDE or editor, and import the following libraries.
35+
1. Create a new Java project in your favorite IDE or editor, and import the following libraries:
3736

3837
```java
3938
import java.net.*;
@@ -46,7 +45,7 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
4645
import com.google.gson.JsonParser;
4746
```
4847

49-
2. Create a new class, with variables for the API endpoint, your subscription key, and search term. You can use the global endpoint below, or the [custom subdomain](../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
48+
2. Create a new class. Add variables for the API endpoint, your subscription key, and search term. 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.
5049

5150
```java
5251
public static SearchResults SearchNews (String searchQuery) throws Exception {
@@ -60,7 +59,7 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
6059

6160
## Construct the search request, and receive a JSON response
6261

63-
1. Use the variables from the last step to format a search URL for the API request. Note that your search term must be URL-encoded before being appended to the request.
62+
1. Use the variables from the previous step to format a search URL for the API request. URL-encode your search term before you append it to the request.
6463

6564
```java
6665
public static SearchResults SearchNews (String searchQuery) throws Exception {
@@ -71,7 +70,7 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
7170
}
7271
```
7372

74-
2. Receive the JSON response from the Bing News Search API, and construct the result object.
73+
2. Receive the JSON response from the Bing News Search API and construct the result object.
7574

7675
```java
7776
// receive JSON body
@@ -84,6 +83,7 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
8483
## Process the JSON response
8584

8685
1. Separate the Bing-related HTTP headers from the JSON body, then close the stream and return the API response.
86+
8787
```java
8888
// extract Bing-related HTTP headers
8989
Map<String, List<String>> headers = connection.getHeaderFields();
@@ -97,7 +97,8 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
9797
return results;
9898
```
9999

100-
2. Create a method to parse and reserialize JSON
100+
2. Create a method to parse and reserialize the JSON results.
101+
101102
```java
102103
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
103104
public static String prettify(String json_text) {
@@ -108,8 +109,9 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
108109
}
109110
```
110111

111-
3. In the main method of your application, call the search method, and display the results.
112-
```csharp
112+
3. In the main method of your application, call the search method and display the results.
113+
114+
```java
113115
public static void main (String[] args) {
114116
System.out.println("Searching the Web for: " + searchTerm);
115117
SearchResults result = SearchNews(searchTerm);
@@ -122,7 +124,7 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
122124
}
123125
```
124126

125-
## JSON Response
127+
## Example JSON response
126128

127129
A successful response is returned in JSON, as shown in the following example:
128130

0 commit comments

Comments
 (0)