Name your function (or class):
Enter the base URL:
Enter the resource/endpoint:
Select authorization type:
Select content type:
Select optional headers:
Select HTTP request method:
Set request parameters:
Key: Value:
Key: Value:
Key: Value:
Key: Value:
Request body (must be valid JSON):
Code samples have been generated in the following languages based on your inputs.
const request = require('request');Test your code on repl.it
const uuidv4 = require('uuid/v4'); const subscriptionKey = process.env.YOUR_ENV_VARIABLE; if (!subscriptionKey) { throw new Error('Environment variable for your subscription key is not set.') }; function {{ functionName }}(){ let options = { method: '{{ httpMethod }}', baseUrl: '{{ baseURL }}', url: '{{ resource }}', qs: { '{{ param.key }}': '{{ param.value }}', }, headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey
'Authorization': 'Bearer YOUR_TOKEN_HERE',
'Content-Type': '{{contentTypeHeader}}',
'X-ClientTraceId': uuidv4().toString() }, body: {{ jsonBody }}, json: true, }; request(options, function(err, res, body){ console.log(JSON.stringify(body, null, 4)); }); }; {{ functionName }}();
import os, requests, uuid, json if 'NAME_OF_YOUR_ENV_VARIABLE' in os.environ: subscriptionKey = os.environ['NAME_OF_YOUR_ENV_VARIABLE'] else: print('Environment variable for your subscription key is not set.') exit() #If you want to set your subscription key as a string, uncomment the line below #and delete the condition statement that starts with if and ends with exit(). #subscriptionKey = 'put_your_key_here' def {{ functionName }}(): base_url = '{{ baseURL }}' path = '{{ resource }}' params = { '{{ param.key }}': '{{ param.value }}', } constructed_url = base_url + path headers = {Test your code on repl.it
'Ocp-Apim-Subscription-Key': subscriptionKey
'Authorization': Bearer YOUR_TOKEN_HERE,
'Content-Type': '{{contentTypeHeader}}',
'X-ClientTraceId': str(uuid.uuid4()) }
body = {{ jsonBody }}
request = requests.{{ httpMethod }}(constructed_url, params=params, headers=headers, json=body) response = request.json() print(json.dumps(response, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ': '))) if __name__ == "__main__": {{ functionName }}()
X-ClientTraceId
for Go at this time.package main import ( "bytes" "encoding/json" "fmt" "log" "net/http" "net/url" "os" ) func main() { subscriptionKey := os.Getenv("YOUR_SUBSCRIPTION_KEY") if subscriptionKey == "" { log.Fatal("Environment variable YOUR_SUBSCRIPTION_KEY is not set.") } {{ functionName }}(subscriptionKey) } func {{ functionName }}(subscriptionKey string) { u, _ := url.Parse("{{ baseURL }}{{ resource }}") q := u.Query() q.Add("{{ param.key }}", "{{ param.value }}") u.RawQuery = q.Encode() body := []struct { Text string }{ {{ jsonBody }} } b, _ := json.Marshal(body) req, err := http.NewRequest("{{ httpMethod | capitalize }}", u.String(), bytes.NewBuffer(b)nil) if err != nil { log.Fatal(err) } req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)req.Header.Add("Authorization", "Bearer YOUR_TOKEN_HERE")Test your code on repl.it
req.Header.Add("Content-Type", "{{ contentTypeHeader }}") res, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err) } var result interface{} if err := json.NewDecoder(res.Body).Decode(&result); err != nil { log.Fatal(err) } prettyJSON, _ := json.MarshalIndent(result, "", " ") fmt.Printf("%s\n", prettyJSON) }
--data-raw '[{"text": "And the world stood still. But only for a single moment."}]'
curl -X {{ httpMethod | capitalize }} '{{ baseURL }}{{ resource }}?{{params[0].key}}={{ params[0].value | encodeUrl }}&{{params[1].key}}={{ params[1].value | encodeUrl }}&{{params[2].key}}={{ params[2].value | encodeUrl }}&{{params[3].key}}={{ params[3].value | encodeUrl }}' \Test your code on repl.it
-H 'Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY' \
-H 'Authorization: Bearer YOUR_TOKEN_HERE' \
-H 'Content-Type: {{contentTypeHeader}}' \
--data-raw '{{ jsonBody }}' | json_pp
X-ClientTraceId
for Java at this time.This Java sample requires Gradle and JDK 8 or later. The samples below provide both a build.gradle.kts
and a Java file.
Follow these instructions to create a Gradle project:
gradle init --type basic
. When prompted, select Kotlin.mkdir -p src/main/java
..java
file. This file must match the public class name in the build and java files.gradle run
to build and run your applicaiton.Gradle Build File
Copy this code into build.gradle.kts
in your working directory. This file was created during initialization.
plugins { java application } application { mainClassName = "{{ functionName }}" } repositories { mavenCentral() } dependencies { compile("com.squareup.okhttp:okhttp:2.5.0") compile("com.google.code.gson:gson:2.8.5") }
Java Code
Copy this code into a file named {{functionName}}.java
in the src/main/java
directory.
import java.io.*; import java.net.*; import java.util.*; import com.google.gson.*; import com.squareup.okhttp.*; public class {{functionName}} { String subscriptionKey = "YOUR_SUBSCRIPTION_KEY"; String url = "{{ baseURL }}{{ resource }}?{{params[0].key}}={{ params[0].value | encodeUrl }}&{{params[1].key}}={{ params[1].value | encodeUrl }}&{{params[2].key}}={{ params[2].value | encodeUrl }}"; OkHttpClient client = new OkHttpClient(); public String {{ httpMethod }}() throws IOException { MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, "{{ jsonBody }}"); Request request = new Request.Builder() .url(url).{{ httpMethod }}(body) .addHeader("Ocp-Apim-Subscription-Key", subscriptionKey).addHeader("Authorization", "Bearer YOUR_ACCESS_TOKEN")Test your code on repl.it
.addHeader("Content-type", "{{contentTypeHeader}}")
.build(); Response response = client.newCall(request).execute(); return response.body().string(); } public static String prettify(String json_text) { JsonParser parser = new JsonParser(); JsonElement json = parser.parse(json_text); Gson gson = new GsonBuilder().setPrettyPrinting().create(); return gson.toJson(json); } public static void main(String[] args) { try { {{functionName}} sampleRequest = new {{functionName}}(); String response = sampleRequest.{{ httpMethod }}(); System.out.println(prettify(response)); } catch (Exception e) { System.out.println(e); } } }