Skip to content

Instantly share code, notes, and snippets.

@akursat
Created February 6, 2020 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akursat/34de057e78f0dc05ce945154a26638ca to your computer and use it in GitHub Desktop.
Save akursat/34de057e78f0dc05ce945154a26638ca to your computer and use it in GitHub Desktop.
grpc-web example
import {
AddCountryRequest,
AddCountryResponse,
GetCountriesRequest,
GetCountriesResponse,
Country
} from "./country_pb";
import { CountryServiceClient } from "./country_grpc_web_pb";
const host = "http://localhost:8080";
const client = new CountryServiceClient(host, null, null);
const metadata = { "Content-Type": "application/grpc-web-text" };
export const getCountries = () => {
let getRequest = new GetCountriesRequest();
client.getCountries(getRequest, metadata, (err, response) => {
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(response.toObject()));
}
});
};
export const addCountry = () => {
let country = new Country();
country.setId(1);
country.setName("djibouti");
country.setFlag(
"https://tr.wikipedia.org/wiki/Cibuti#/media/Dosya:Flag_of_Djibouti.svg"
);
let addRequest = new AddCountryRequest();
addRequest.setCountry(country);
client.addCountry(addRequest, metadata, (err, response) => {
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(response.toObject()));
}
});
};
getCountries();
addCountry();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment