POST sample clients

parent cdf6c0f9
# Εισαγωγή
Για τα παρακάτω παραδείγματα κατανάλωσης API έχουν γίνει οι εξής παραδοχές:
1. Ο server που παρέχει το API έχει τη διεύθυνση **wso2.local.dev**
2. Η πρόσβαση στα API με πρωτόκολλο **HTTP** γίνεται στην πόρτα **8280**
3. Η πρόσβαση στα API με πρωτόκολλο **HTTPS** γίνεται στην πόρτα **8243**
4. Το δοκιμαστικό API καλείται **identity**, έκδοση **1.0.0**
5. Όπου απαιτείται Authirization header, χρησιμοποιείται το **```<token>```**
## Κλήση POST API resource "details"
Το συγκεκριμένο POST API resource
- ονομάζεται **details**
- δέχεται τις παραμέτρους:
- **id** (κείμενο)
- **fields** (κείμενο)
- δέχεται δεδομένα σε μορφή **JSON**
- καλείται με μέθοδο κλήσης **POST**
**Δείγματα**
| Γλώσσα | Αρχείο |
| ------ | ------ |
| Command line curl | [details_post.sh](sh/details_post.sh) |
| PHP | [details_post.php](php/details_post.php) |
| Java | [details_post.java](java/details_post.java) |
| Javascript | [details_post.js](javascript/details_post.js) [details_ajax_post.js](javascript/details_ajax_post.js)|
## Κλήση GET API resource "verify"
**Υπό αναθεώρηση**
Το συγκεκριμένο GET API resource
- ονομάζεται **verify**
- δέχεται την παράμετρο:
- **id** (κείμενο)
- δέχεται δεδομένα στο query string
- καλείται με μέθοδο κλήσης **GET**
package gr.gov.minedu.osteam.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.Base64;
import java.security.*;
public class WebServiceClient {
public static String clientConnectGetJson() {
StringBuilder sb = new StringBuilder();
try {
String resourceEndpoint = "https://wso2.local.dev:8243/identity/1.0.0/details";
String JSONobject = "{ \"id\": \"0123\", \"fields\": \"id,fullname,organisation\" }";
String auth = "Bearer " + "<token>";
URL url = new URL(resourceEndpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", auth);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("User-Agent", "WSO2 sample java caller");
connection.setUseCaches(false);
connection.setConnectTimeout(50000);
connection.setReadTimeout(50000);
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(JSONobject); // send the payload
out.close();
int HttpResult = connection.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
} else {
return "Client connection error:" + connection.getResponseMessage().toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
return "WebServiceClient MalformedURLException error:"+e.toString();
} catch (IOException e) {
e.printStackTrace();
return "WebServiceClient IOException error:"+e.toString();
} catch (Exception ex) {
return "WebServiceClient Exception error:"+ex.toString();
} finally {
if (WebServiceClient.connectionURL != null) {
WebServiceClient.connectionURL.disconnect();
}
}
return null;
}
}
$.ajax({
type: 'POST',
url: 'https://wso2.local.dev:8243/identity/1.0.0/details',
dataType: "json",
data: {"id" : "0123", "fields" : "id,fullname,organisation"},
beforeSend: function(req) {
req.setRequestHeader("Authorization", "Bearer <token>");
},
success: function(data){
console.log(data);
}
});
var params = JSON.stringify({"id": "0123", "fields": "id,fullname,organisation"});
var http = new XMLHttpRequest();
http.open("POST", "https://wso2.local.dev:8243/identity/1.0.0/details");
http.setRequestHeader("Content-type", "application/json; charset=utf-8");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Accept", "application/json");
http.setRequestHeader("Authorization", "Bearer <token>");
http.setRequestHeader("User-Agent", "WSO2 sample javascript caller");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
}
}
http.send(params);
<?php
$auth = "Bearer <token>";
$payload = json_encode([
"id" => "0123",
"fields" => "id,fullname,organisation"
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://wso2.local.dev:8243/identity/1.0.0/details");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: {$auth}",
"Content-Type: application/json",
"Accept: application/json",
"User-Agent: WSO2 sample php caller"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo "Error: ", curl_error($ch),
"Curl info: ", var_export(curl_getinfo($ch), true);
$result = false;
} else {
// possibly parse result...
$result = json_decode($result, true);
}
curl_close($ch);
return $result;
#!/bin/sh
curl -X POST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--header "Authorization: Bearer <token>" \
--header "User-Agent: WSO2 sample command line caller" \
-d '{ "id": "0123", "fields": "id,fullname,organisation" }' \
'https://wso2.local.dev:8243/identity/1.0.0/details'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment