Java Code Examples for com.mashape.unirest.http.Unirest#put()

The following examples show how to use com.mashape.unirest.http.Unirest#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestAction.java    From cognitivej with Apache License 2.0 7 votes vote down vote up
private HttpRequest buildUnirest(WorkingContext workingContext) {
    String url = String.format("%s%s", PROJECTOXFORD_AI, workingContext.getPathBuilt());
    switch (workingContext.getHttpMethod()) {
        case GET:
            return Unirest.get(url);
        case PUT:
            return Unirest.put(url);
        case DELETE:
            return Unirest.delete(url);
        case PATCH:
            return Unirest.patch(url);
    }
    return Unirest.post(url);
}
 
Example 2
Source File: AbstractRequest.java    From alpaca-java with MIT License 5 votes vote down vote up
/**
 * Invoke put http response.
 *
 * @param abstractRequestBuilder the abstract request builder
 *
 * @return the http response
 */
public HttpResponse<InputStream> invokePut(AbstractRequestBuilder abstractRequestBuilder) {
    try {
        String url = abstractRequestBuilder.getURL();

        LOGGER.debug("PUT URL " + url);

        HttpRequestWithBody request = Unirest.put(url);

        if (!headers.isEmpty()) {
            request.headers(headers);

            LOGGER.debug("PUT Headers: " + headers);
        }

        String body = abstractRequestBuilder.getBody();
        if (body != null) {
            request.body(body);

            LOGGER.debug("PUT Body: " + body);
        }

        return request.asBinary();
    } catch (UnirestException e) {
        LOGGER.error("UnirestException", e);
    }

    return null;
}
 
Example 3
Source File: PacketBuilder.java    From discord.jar with The Unlicense 5 votes vote down vote up
public String makeRequest() {
  	try {
  		HttpRequestWithBody request;
      	
      	switch (type) {
  			case DELETE:
  				request = Unirest.delete(url);
  				break;
  			case OPTIONS:
  				request = Unirest.options(url);
  				break;
  			case PATCH:
  				request = Unirest.patch(url);
  				break;
  			case POST:
  				request = Unirest.post(url);
  				break;
  			case PUT:
  				request = Unirest.put(url);
  				break;
  			case GET:
  				return Unirest.get(url).header("authorization", "Bot " + api.getLoginTokens().getToken()).header("Content-Type", isForm ? "application/x-www-form-urlencoded" : (file ? "application/octet-stream" : "application/json; charset=utf-8")).asString().getBody();
  			default:
  				throw new RuntimeException();
  		}
      	return request.header("authorization", "Bot " + api.getLoginTokens().getToken()).header("Content-Type", isForm ? "application/x-www-form-urlencoded" : (file ? "application/octet-stream" : "application/json; charset=utf-8")).body(data).asString().getBody();
} catch (UnirestException e) {
	throw new RuntimeException(e);
}
  	
  }
 
Example 4
Source File: CustomHttpClient.java    From openvidu with Apache License 2.0 5 votes vote down vote up
private JsonObject commonRest(HttpMethod method, String path, String body, int status) throws Exception {
	HttpResponse<?> jsonResponse = null;
	JsonObject json = null;
	path = openviduUrl + (path.startsWith("/") ? path : ("/" + path));

	HttpRequest request = null;
	if (body != null && !body.isEmpty()) {
		switch (method) {
		case POST:
			request = Unirest.post(path);
			break;
		case PUT:
			request = Unirest.put(path);
			break;
		case PATCH:
		default:
			request = Unirest.patch(path);
			break;
		}
		((HttpRequestWithBody) request).header("Content-Type", "application/json").body(body.replaceAll("'", "\""));
	} else {
		switch (method) {
		case GET:
			request = Unirest.get(path);
			request.header("Content-Type", "application/x-www-form-urlencoded");
			break;
		case POST:
			request = Unirest.post(path);
			break;
		case DELETE:
			request = Unirest.delete(path);
			request.header("Content-Type", "application/x-www-form-urlencoded");
			break;
		case PUT:
			request = Unirest.put(path);
		default:
			break;
		}
	}

	request = request.header("Authorization", this.headerAuth);
	try {
		jsonResponse = request.asJson();
		if (jsonResponse.getBody() != null) {
			jsonResponse.getBody();
			json = JsonParser.parseString(((JsonNode) jsonResponse.getBody()).getObject().toString())
					.getAsJsonObject();
		}
	} catch (UnirestException e) {
		try {
			if (e.getCause().getCause().getCause() instanceof org.json.JSONException) {
				try {
					jsonResponse = request.asString();
				} catch (UnirestException e1) {
					throw new Exception("Error sending request to " + path + ": " + e.getMessage());
				}
			} else {
				throw new Exception("Error sending request to " + path + ": " + e.getMessage());
			}
		} catch (NullPointerException e2) {
			throw new Exception("Error sending request to " + path + ": " + e.getMessage());
		}
	}

	if (jsonResponse.getStatus() == 500) {
		log.error("Internal Server Error: {}", jsonResponse.getBody().toString());
	}

	if (status != jsonResponse.getStatus()) {
		System.err.println(jsonResponse.getBody().toString());
		throw new Exception(path + " expected to return status " + status + " but got " + jsonResponse.getStatus());
	}

	return json;
}
 
Example 5
Source File: AppServicePacketTransport.java    From swellrt with Apache License 2.0 5 votes vote down vote up
private BaseRequest formRequest(Request packet) {
  HttpRequest httpRequest = null;

  String url = "https://" + serverAddress + apiAddress + packet.getUrl();

  String method = packet.getMethod();
  switch (method) {
    case "GET":
      httpRequest = Unirest.get(url);
      break;

    case "POST":
      httpRequest = Unirest.post(url);
      break;

    case "PUT":
      httpRequest = Unirest.put(url);
      break;
  }

  Map<String, String> headers = packet.getHeaders();
  httpRequest = httpRequest.headers(headers);

  Map<String, Object> queryStrings = packet.getQueryStrings();
  httpRequest = httpRequest.queryString(queryStrings);

  BaseRequest request = httpRequest;

  if(!method.equals("GET")) {
    JSONObject body = packet.getBody();
    request = ((HttpRequestWithBody)request).body(body.toString());
  }

  return request;
}
 
Example 6
Source File: BaseTestCase.java    From blade with Apache License 2.0 4 votes vote down vote up
protected HttpRequest put(String path) {
    log.info("[PUT] {}", (origin + path));
    return Unirest.put(origin + path);
}