How to use the got.post function in got

To help you get started, we’ve selected a few got examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nasa / cumulus / daac-ops-api / endpoints / token.js View on Github external
function token(event, context) {
  const EARTHDATA_CLIENT_ID = process.env.EARTHDATA_CLIENT_ID;
  const EARTHDATA_CLIENT_PASSWORD = process.env.EARTHDATA_CLIENT_PASSWORD;

  const EARTHDATA_BASE_URL = process.env.EARTHDATA_BASE_URL || 'https://uat.urs.earthdata.nasa.gov';
  const EARTHDATA_CHECK_CODE_URL = `${EARTHDATA_BASE_URL}/oauth/token`;

  const code = get(event, 'queryStringParameters.code');
  const state = get(event, 'queryStringParameters.state');

  // Code contains the value from the Earthdata Login redirect. We use it to get a token.
  if (code) {
    const params = `?grant_type=authorization_code&code=${code}&redirect_uri=${redirectUriParam()}`;

    // Verify token
    return got.post(EARTHDATA_CHECK_CODE_URL + params, {
      json: true,
      auth: `${EARTHDATA_CLIENT_ID}:${EARTHDATA_CLIENT_PASSWORD}`
    }).then((r) => {
      const tokenInfo = r.body;
      const accessToken = tokenInfo.access_token;

      // if no access token is given, then the code is wrong
      if (typeof accessToken === 'undefined') {
        return resp(context, new Error('Failed to get Earthdata token'));
      }

      const refresh = tokenInfo.refresh_token;
      const userName = tokenInfo.endpoint.split('/').pop();
      const expires = (+new Date()) + (tokenInfo.expires_in * 1000);

      const u = new User();
github kabirvirji / spotifork / index.js View on Github external
if (playlistName === undefined){
	    	playlistName = response.body.name;
	    }
	    // holds playlist tracks
		let tracks = responseTracks.map(responseTrack => responseTrack.track.uri)
		var createPlaylistOptions = {
		  json: true, 
		  headers: {
		    'Content-type': 'application/json',
		    'Authorization' : `Bearer ${config.get('bearer')}`,
		    'Accept' : 'application/json'
		  },
		  body: JSON.stringify({ description: `spotiforked from ${playlistUser}/${forkedFrom}`, name: `${playlistName}`, public : true})
		};
		// create playlist
		got.post(`https://api.spotify.com/v1/users/${config.get('username')}/playlists`, createPlaylistOptions)
		  .then(response => {
		  	// get playlist ID
		    const newPlaylistID = response.body.id;

				// function to add tracks to playlist
				function populatePlaylist (id, uris, name) {
					var url = `https://api.spotify.com/v1/users/${config.get('username')}/playlists/${id}/tracks?uris=${uris}`
					var options = {
					  json: true, 
					  headers: {
					    'Content-type': 'application/json',
					    'Authorization' : `Bearer ${config.get('bearer')}`,
					  }
					};
					got.post(url, options)
					  .then(response => {
github welovekpop / munar / packages / munar-plugin-emotes / src / index.js View on Github external
async upload (form) {
    const { body } = await got.post('https://api.imgur.com/3/image', {
      headers: {
        Authorization: `Client-ID ${this.options.key}`
      },
      body: form,
      form: true,
      json: true
    })
    if (body && body.success) {
      return body.data.link.replace('http:', 'https:')
    } else {
      console.log(body)
    }
  }
github nasa / cumulus / packages / api / lib / EarthdataLogin.js View on Github external
requestAccessToken(authorizationCode) {
    return got.post(
      this.tokenEndpoint(),
      {
        json: true,
        form: true,
        body: {
          grant_type: 'authorization_code',
          code: authorizationCode,
          redirect_uri: this.redirectUri.toString()
        },
        auth: `${this.clientId}:${this.clientPassword}`
      }
    );
  }
github ethereum / mist / modules / sockets / web3Http.js View on Github external
_call(dataStr) {
    return got
      .post(this._hostPort, {
        encoding: this._encoding,
        headers: {
          'Content-Type': 'application/json'
        },
        body: dataStr
      })
      .then(res => {
        return res.body;
      });
  }
}
github electrode-io / electrode-native / ern-core / src / BundleStoreSdk.ts View on Github external
sourceMapPath,
    store,
  }: {
    accessKey: string
    bundlePath: string
    platform: string
    sourceMapPath: string
    store: string
  }): Promise {
    try {
      const bundleFileRs = fs.createReadStream(bundlePath)
      const sourceMapFileRs = fs.createReadStream(sourceMapPath)
      const form = new FormData()
      form.append('bundle', bundleFileRs)
      form.append('sourcemap', sourceMapFileRs)
      const res = await got.post(
        `http://${this.host}/bundles/${store}/${platform}`,
        {
          ...this.gotCommonOpts,
          body: form,
          headers: {
            'ERN-BUNDLE-STORE-ACCESS-KEY': accessKey,
          },
        }
      )
      return JSON.parse(res.body).id
    } catch (err) {
      throw new Error(err.response?.text ?? err.message)
    }
  }
github SIGSEV / Bridge / api / services / deluge.js View on Github external
const r = (host, body, headers = {}) =>
  got.post(`${host}/json`, {
    body,
    headers: { ...headers, 'Content-Type': 'application/json' },
    json: true,
  })
github sourcegraph / sourcegraph-typescript / src / server / graphql.ts View on Github external
export async function requestGraphQL(
    query: string,
    variables: any = {},
    { instanceUrl, accessToken }: Options
): Promise<{ data?: any; errors?: { message: string; path: string }[] }> {
    const headers: Record = {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'User-Agent': 'TypeScript language server',
    }
    if (accessToken) {
        headers.Authorization = 'token ' + accessToken
    }
    const response = await got.post(new URL('/.api/graphql', instanceUrl), {
        headers,
        body: { query, variables },
        json: true,
    })
    return response.body
}
github parkroolucas / microservices-demo / api-gateway / src / adapters / UsersService.js View on Github external
static async createUser({ email, password }) {
    const body = await got.post(`${USERS_SERVICE_URI}/users`, { json: { email, password } }).json();
    return body;
  }
github parkroolucas / microservices-demo / api-gateway / src / adapters / UsersService.js View on Github external
static async createUserSession({ email, password }) {
    const body = await got.post(`${USERS_SERVICE_URI}/sessions`, { json: { email, password } }).json();
    return body;
  }