Inputs

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

Code samples have been generated in the following languages based on your inputs.


const request = require('request');
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: { }, 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 }}();
Test your code on repl.it

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 = {
    }

    constructed_url = base_url + path

    headers = {
'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 }}()
Test your code on repl.it

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()
    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")
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) }
Test your code on repl.it

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 }}' \
-H 'Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY'
\
-H 'Authorization: Bearer YOUR_TOKEN_HERE'
\
-H 'Content-Type: {{contentTypeHeader}}'
\
--data-raw '{{ jsonBody }}'
| json_pp
Test your code on repl.it

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:

  • Create a new folder or navigate to the working directory for your project.
  • Run: gradle init --type basic. When prompted, select Kotlin.
  • From your working directory, run this command to create the necessary folder structure: mkdir -p src/main/java.
  • In this folder, create a new .javafile. This file must match the public class name in the build and java files.
  • After you've copied the code into your project, use 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")
.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); } } }
Test your code on repl.it