Showing posts with label PUT request. Show all posts
Showing posts with label PUT request. Show all posts

Friday, 20 September 2019

RestTemplate: PUT Request Example


Below snippet performs PUT request.

public static ResponseEntity<String> put(String url, Map<String, String> headers, String json) {
 if (url == null || url.isEmpty()) {
  return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("URL must not be null");
 }

 HttpHeaders httpHeaders = getHeadersFromMap(headers);
 //httpHeaders.setContentType(MediaType.APPLICATION_JSON);

 HttpEntity<Object> httpEntity = new HttpEntity<Object>(json, httpHeaders);

 return REST_TEMPLATE.exchange(url, HttpMethod.PUT, httpEntity, String.class);

}


You can download complete working application from this link.

Previous                                                    Next                                                    Home

Sunday, 18 August 2019

HTTPie: PUT: Update an entity


Syntax
http PUT {url} key1:value1 key2:valu2….keyN:valueN

Example
http PUT http://localhost:8080/public/v1/guests/ id=1 firstName=Siva lastName=Nzv

Above request is equivalent to below one.

PUT /public/v1/guests/ HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 43
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/1.0.2

{
    "id": 1,
    "firstName": "Siva",
    "lastName": "Nzv"
}

Create a guest
$http POST http://localhost:8080/public/v1/guests/ firstName=krishna lastName=maj
HTTP/1.1 200 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 11:41:07 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "firstName": "krishna",
    "id": 5,
    "lastName": "maj"
}


Get the Guest by id
$http GET http://localhost:8080/public/v1/guests/5
HTTP/1.1 200 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 11:41:55 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "firstName": "krishna",
    "id": 5,
    "lastName": "maj"
}


Update the guest with id 5

$http PUT http://localhost:8080/public/v1/guests/ id=5 firstName=Siva lastName=Nzv
HTTP/1.1 200 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 11:44:04 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "firstName": "Siva",
    "id": 5,
    "lastName": "Nzv"
}

$
$http GET http://localhost:8080/public/v1/guests/5
HTTP/1.1 200 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 11:44:09 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "firstName": "Siva",
    "id": 5,
    "lastName": "Nzv"
}



Previous                                                    Next                                                    Home